收到发送邮件错误

时间:2011-11-23 09:33:42

标签: c# smtpclient

有没有办法从smtp发送错误以检查邮件是否成功发送?

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

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

此致 梅尔。

3 个答案:

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

答案 1 :(得分:0)

这种错误具有不同步的性质。发送邮件时,您可以与提供商的本地smtp服务器通信。之后该服务器开始将邮件传递到目标邮件系统。

因此,SmtpClient类只能显示与本地smtp服务器通信时发生的错误。

通常,当目标系统上出现“未知用户”等错误时,它会向发件人电子邮件地址发送包含失败消息的电子邮件。

答案 2 :(得分:0)

这篇文章对我有帮助。

顺便说一句,如果您使用的是.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"));
        }
    }