Get sent mail error

牧云@^-^@ 提交于 2019-12-08 14:04:48

问题


Is there any way to get sent error from the smtp to check if the mail is sent successfully?

var smtpClient = new SmtpClient("SmtpServer");
                smtpClient.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
smtpClient.SendAsync(mail, userId);

The errors I am looking for are: mail can't be deliver because the mail address not exists, mail box full etc...

Regards, Meir.


回答1:


I am not sure what you are trying to achieve but this will helps you.

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



回答2:


Such kind of errors have a asnychronous nature. When sending mail you talk to the local smtp server of your provider. That server afterwards starts to deliver the mail to the target mail system.

So the SmtpClient class can only show you errors occuring while talking to your local smtp server.

Typically when an error like "unknown user" occures on the target system, it will send an email with the failure message to the originator email address.




回答3:


This post is helpful to me.

By the way if you're using .net 4.0 this one will be the changes on the above code. Sorry for my first post i don't know why it appears that way.

Here's the code:

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   
                //for .net 4.0
                //statusCode = exObj.InnerExceptions(recipient).StatusCode;

                statusCode = exObj.StatusCode;

                //if (((statusCode == Net.Mail.SmtpStatusCode.MailboxBusy) || (statusCode == Net.Mail.SmtpStatusCode.MailboxUnavailable)))
                //for .net 4.0
                if (((statusCode == System.Net.Mail.SmtpStatusCode.MailboxBusy)
                            || (statusCode == System.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"));
        }
    }


来源:https://stackoverflow.com/questions/8239881/get-sent-mail-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!