收到发送邮件错误

本文关键字:错误 | 更新日期: 2023-09-27 18:15:25

是否有办法从smtp获得发送错误来检查邮件是否发送成功?

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

我正在寻找的错误是:邮件无法投递,因为邮件地址不存在,邮箱已满等…

问候,梅尔。

收到发送邮件错误

我不知道你想达到什么目的,但这会对你有所帮助。

我想你已经知道System.Net.Mail.MailMessage上的DeriveryNotificationOptions property了。使用该属性唯一棘手的部分是它的enum类型表示位字段,因此应该将其设置为希望应用的选项的总和。

例如,如果您想要延迟、失败或成功的传递通知,您应该将属性设置为

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

这类错误具有异步性。当发送邮件时,您与提供商的本地smtp服务器进行通信。然后,该服务器开始将邮件发送到目标邮件系统。

所以SmtpClient类只能显示在与本地smtp服务器通信时发生的错误。

一般情况下,当目标系统上出现"未知用户"之类的错误时,它将向发送者的电子邮件地址发送包含失败消息的电子邮件。

这篇文章对我很有帮助。

顺便说一下,如果你使用的是。net 4.0,这将是上述代码的变化。很抱歉我的第一个帖子,我不知道为什么会这样。

代码如下:

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