在网络应用程序中,如何找到邮件是否未送达。我编写了发送邮件的代码,我可以获得邮件发送失败报告的报告。
objMialBe.Empid = Session["empid"].ToString();
string myHost = System.Net.Dns.GetHostName();
objMialBe.IpAddress = System.Net.Dns.GetHostByName(myHost).AddressList[0].ToString();
objMialBe.Subject = txtSubject.Text;
objMialBe.Mbody = txtDescri.Text;
objMialBe.ValidDays = Convert.ToInt32(txtNoDays.Text);
objMialBe.SizeInBytes = Tsize.ToString();
objMialBe.TempAsizebbyte = TotSize;
objMialBe.TemsizeMb = TotSizeMB;
objMialBe.SessionId = Session.SessionID.ToString();
int k = ObjMailBl.Insert_Mails_Sent(objMialBe); // inserting data in table Tbl_Mails_sent
string TO = txtTo.Text;
string[] inputArray = TO.Split(';');
for (int i = 0; i < inputArray.Length; i++)
{
int cnt = 1;
//// Response.Write("#" + i + ": " + inputArray[i] + "<br>");
MailMessage mail = new MailMessage();
string t = txtTo.Text;
mail.To.Add(inputArray[i]);
mail.To.Add(t);
mail.From = new MailAddress("iilwebadmin@indimmune.com");
mail.Subject = txtSubject.Text;
StringBuilder stbldr = new StringBuilder();
stbldr.Append("<html>" + txtDescri.Text + "</html>");
if (item >= 1)
{
foreach (DataListItem dl in dtlstTemp.Items)
{
Label latt = (Label)dl.FindControl("lblFileAtt");
stbldr .Append ("<html> <a href='http://8888888/*******/mails/CheckAttachment.aspx?attid="+s+"&Eid="+txtTo.Text+"&afile="+latt.Text +"' target='_blank'>Attachment..</a> "+cnt+" <br> </html>");
cnt++;
}
}
mail.Body = stbldr.ToString();
SmtpClient smtp = new SmtpClient("**********", 25);
smtp.Send(mail);
答案 0 :(得分:1)
我假设您已经了解DeriveryNotificationOptions property
上的System.Net.Mail.MailMessage
。使用该属性的唯一棘手的部分是它的枚举类型表示一个位域,因此您应该将其设置为要应用的选项的总和。
例如,如果您想要延迟,失败或成功的递送通知,则应将该属性设置为
DeliveryNotificationOptions.Delay + DeliveryNotificationOptions.OnFailure + DeliveryNotificationOptions.OnSuccess
或
这是捕获失败报告的一种方法或未发送邮件时的任何错误(失败报告)
// Change your Try-Catch to call the new method named 'CheckExceptionAndResend'
// Error handling for sending message
try
{
smtpClient.Send(message);
// Exception contains information on each failed receipient
}
catch (System.Net.Mail.SmtpFailedRecipientsException recExc)
{
// Call method that will analyze exception and attempt to re-send the email
CheckExceptionAndResend(recExc, smtpClient, message);
}
catch (System.Net.Mail.SmtpException smtpExc)
{
// Log error to event log using StatusCode information in
// smtpExc.StatusCode
MsgBox((smtpExc.StatusCode.ToString + " ==>Procedure SmtpException"));
}
catch (Exception Exc)
{
// Log error to event log using StatusCode information in
// smtpExc.StatusCode
MsgBox((Exc.Message + " ==>Procedure Exception"));
}
private void CheckExceptionAndResend(System.Net.Mail.SmtpFailedRecipientsException exObj, System.Net.Mail.SmtpClient smtpClient, MailMessage emailMessage)
{
try
{
for (int recipient = 0; (recipient <= (exObj.InnerExceptions.Length - 1)); recipient++)
{
System.Net.Mail.SmtpStatusCode statusCode;
// Each InnerException is an System.Net.Mail.SmtpFailed RecipientException
statusCode = exObj.InnerExceptions(recipient).StatusCode;
if (((statusCode == Net.Mail.SmtpStatusCode.MailboxBusy)
|| (statusCode == Net.Mail.SmtpStatusCode.MailboxUnavailable)))
{
// Log this to event log: recExc.InnerExceptions(recipient).FailedRecipient
System.Threading.Thread.Sleep(5000);
smtpClient.Send(emailMessage);
}
else
{
// Log error to event log.
// recExc.InnerExceptions(recipient).StatusCode or use statusCode
}
}
MsgBox((exObj.Message + " ==>Procedure SmtpFailedRecipientsException"));
}
catch (Exception ex)
{
// At this point we have an non recoverable issue:
// NOTE: At this point we do not want to re-throw the exception because this method
// was called from a 'Catch' block and we do not want a hard error to display to the client.
// Options: log error, report issue to client via msgbox, etc. This is up to you.
// To display issue as you have before:
MsgBox((exObj.Message + " ==>Email was not sent"));
}
}