Artificial Insemination: Nature and Scope:
“In AIH or Artificial Insemination using Husband’s semen, the husband does not have living spermatozoa in his semen. This approach is obviously hopeless. However, when the husband has normal spermatozoa but for some reason is not able to deposit them inside the genital tract of the wife, artificial insemination might solve the problem.
It may happen that the seminal ejaculate lacks the proper concentration of live sperm, so that it becomes necessary to repeatedly collect the first wave of every ejaculate, which is the richest in sperms, and prepare a satisfactory concentrate to be used. This can be kept in cold storage, to be drawn from at the time of ovulation each cycle and deposited by the doctor inside the genital tract until hopefully a pregnancy results.”
Thus, in artificial insemination, bearing babies and giving birth to children is achieved in a non-natural way (the natural way being direct sexual contact.
An Islamic Approach to the Issue in General
Dr. MuzammilSiddiqi, former president of the Islamic Society of North America, states:

Indeed, artificial insemination is one of the new issues on which Muslim scholars have recently done some Ijtihad in the light of some basic principles and values of the Qur’an and Sunnah.
artificial insemination for conceptual purpose is generally needed in the situation when the husband is not able to deposit his semen inside his wife’s genital tract. This procedure is allowed in Islam as long as it is between legally married couples during the life of the husband. The jurists have emphasized that under the Shari`ah, a wife is not allowed to receive the semen of her ex-husband after divorce or after his death.

The Islamic Reservations against Third Party’s Involvement
Stressing the irreparable harms that occur as a result of another donor’s involvement in the process, it must be borne in mind that “Islam safeguards lineage by prohibiting Zina and legal adoption, thus keeping the family line unambiguously defined without any foreign element entering into it. It likewise pr
ohibits what is known as artificial insemination if the donor of the semen is other than the husband.
With regard to this, the late well-known Grand Imam of Azhar, Sheikh MahmoudShaltut, states:

It is a despicable crime and a major sin which deserves also to be classified in the same category of adultery. Both (adultery and artificial insemination by anyone other than the husband) are similar in nature and in effect; that is, in both cases the tillage, which belongs exclusively to the husband, is intentionally inseminated by a stranger. Had the form of this crime not been of a lesser degree, such insemination would have been punishable by the same legal punishment or Hadd as is prescribed for adultery in the Shari`ah.
Moreover, there is no doubt that insemination by a donor other than the husband is a more serious crime and detestable offense than adoption, for the child born of such insemination incorporates in itself the result of adoption – the introduction of an alien element into the lineage – in conjunction with the offence of adultery, which is abhorrent both to the divinely revealed laws and to upright human nature.
By this action the human being is degraded to the level of an animal, who has no consciousness of the noble bonds (of morality and lineage, which exist among the members of a human society.

Stressing the aforementioned facts, the prominent Saudi Islamic lecturer and author, Sheikh Muhammad Saleh Al-Munajjid, adds:

If a third party, other than the spouses, involves in this process, such as when the sperm comes from another man, then fertilization in such cases is unlawful, because it is counted as Zina or adultery.
With regard to the child born as the result of this, he is to be attributed to the mother who bore him, and not to the man who produced the sperm, as is the ruling in the case of Zina (fornication or adultery).
If that man claims to be the father and no one disputes that, then the child may be attributed to him, because the Lawgiver is keen that people should be named after their biological fathers. The Prophet, peace and blessings be upon him, is reported to have said, “The child belongs to the bed and for the adulterer is the stone”

Moreover, Sheikh Ahmad Kutty, a Senior Lecturer and an Islamic Scholar at the Islamic Institute of Toronto, Ontario, Canada, says on this:

Scholars and jurists have discussed this issue, which is most commonly known as in-vitro fertilization, and they have concluded that it is perfectly acceptable within the boundaries of marriage to do so. In other words, it is permissible so long as both the sperm and egg involved in the procedure come from the SAME married spouses. Thus, if the husband’s sperm is extracted and it has been fertilized with the egg of the spouse inside a test tube, and then it has been implanted into the spouse’s womb for conception, that is perfectly acceptable according to the teachings of the Shari`ah.
While approving the above method of conception, scholars have unanimously condemned the procedure in case a third party is introduced into the equation: That would be the case if either the sperm or egg involved in the above process were obtained from either man or woman who is not related to each other in marriage.

Safe Lab Conditions Have to Be Insured
In his commentary on this point, Dr.`Abdul-Fattah Idrees, Professor of Islamic Jurisprudence at Al-Azhar University, states:

Artificial insemination should be conducted under meticulous as well as safe laboratory conditions.
the owners of those labs should be trustworthy people. The board of the Islamic Fiqh Council have agreed on the issue of artificial insemination as long as no other third party is involved. The council also stressed the necessity of carrying out the operation when both the husband and wife are alive. In supporting their view, the council cited the Hadith in which the Prophet, peace and blessings be upon him, is reported to have said: “No one of you should lag behind in seeking progeny because if he dies while having no children, his traces will be wiped out.”
the Council states that there is nothing wrong in seeking lawful means of having children as long as nothing Haram is involved in the process of fertilization such as the confusion of lineage or the possibility of another man’s egg entering the woman’s vagina by mistake. Thus, the insermination is permissible as long as the aforementioned guidelines are observed, irrespective of whether the process of fertilization is carried out inside or outside the woman’s vagina.

Artificial Insemination by a Donor’s Semen (AID)
In this situation, the husband is in fact infertile and does not possess semen of his own that is ever capable of producing a pregnancy; this is the case in men whose semen contains no spermatozoa and there is no known treatment that can correct their defect. Resort is then made to semen given by a fertile donor.
A ‘semen bank’ carries out the function of obtaining seminal ejaculates from healthy fertile donors, and preserving them at a very low temperature. The donors are medically checked to exclude diseases communicable by semen (lately AIDS -acquired immune deficiency syndrome – has been added to the checklist).
the donors and recipients remain unknown to each other and written consent is taken from the recipient and her husband. Although the procedure can put an end to the problem of the fertile wife of an infertile husband, it stands unacceptable in Islam.
excerpted, with slight modifications, from:
As to the point you have raised concerning the jailed husband, the eminent Azharite scholar, Sheikh `Abdul-MajeedSubh, states:

There is nothing wrong, as far as Islam is concerned, in carrying out the process of artificial insemination while the husband is jailed provided that the semen is his, and there is no third party involved, otherwise, it is not permissible to carry out the operation.
as to the case where the husband’s semen got mixed with that of another person during the process of insemination, but both the husband and wife discovered this later after delivering the baby, I can say that such a mixture rarely happens. However, both the husband and wife should be keen to carry out the process in a safe laboratory condition that do secure and ensure that no semen of a third party is mixed with that of the husband. If such a mixture takes place, it denotes that there is some sort of cheating from the lab authority or there is negligence from the part of the spouses. Thus, it is not allowed to carry out the process of artificial insemination without the availability of those securing conditions.

Concerning using the dead husband’s frozen sperm after his death, Sheikh `Abdul-Khaleq Hassan Ash-Shareef, the promient Muslim scholar and Da`iyah states the following:

It is not permissible for the woman to use the frozen sperm of her husband after divorce or after his death. The issue is permissible only when both the husband and wife are living together (i.e. they are not divorced) and both of them are alive.
Hence, it is not permissible for the ex-wife or the widow to use her husband’s frozen sperm after his death or after divorce. The woman may face a very difficult situation as regards this, especially when she gets pregnant, while her husband is dead or she is divorced.

In addition, the Fatwa issued by The European Council for Fatwa and Research states:

It is permissible for the wife to use the sperm of her husband for fertilization unless she is divorced or the husband dies.
In case the wife is separated from her husband (i.e. by divorce or death), it’s permissible for her then to get rid of the frozen sperm or its remnants.

In the light of this comprehensive fatwa, it’s clear that what the `Ulama focus on for the artificial insemination to keep its permissibility is to be conducted in a safe atmosphere where no slight cheating or deception takes place, and third party should not get involved in this process.