问题描述:

In web application, How can i find if mail is not delivered. I write code for sending mail, can i get the report of mail deliver failure report.

 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("[email protected]");

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);

网友答案:

I assume you're already aware of the DeriveryNotificationOptions property on System.Net.Mail.MailMessage. The only tricky part to using that property is that its enum type represents a bitfield, so you should set it to the sum of the options you want to apply.

For example, if you want delivery notification on delay, failure, or success, you should set the property to

DeliveryNotificationOptions.Delay + DeliveryNotificationOptions.OnFailure + DeliveryNotificationOptions.OnSuccess

Or

this is one method to capture the failure report or any error when the mail has not been sent (failure report)

 // 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"));
    }
}
相关阅读:
Top