简体   繁体   English

如何知道在C#中发送失败邮件

[英]How to know sending failure mails in c#

public bool SendEmailToActivate(string email)
        {
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

                mail.From = new MailAddress("mymailid@gmail.com");
                mail.To.Add("sendingmailid@gmail.com");
                mail.Subject = "Test Mail";
                mail.Body = "This is for testing SMTP mail from GMAIL";

                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("mymailid@gmail.com", "password");
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

how to know the mail is send success to the user or not in c#. 如何在C#中知道邮件是否成功发送给用户。 ie. 即。 if the mail is not send to user then i want the count of the failure mails.. please help me. 如果邮件没有发送给用户,那么我要计数失败邮件..请帮助我。

A good start to your solution would be to actually utilize what catch can do, and what the various Exception classes provide in feedback. 解决方案的一个好的开始是实际利用catch可以做什么,以及各种Exception类在反馈中提供的功能。

Your current implementation does nothing for handling. 您当前的实现对处理没有任何作用。 IntelliSense probably has flagged ex as not being used IntelliSense可能已将ex标记为未使用

catch (Exception ex) {
    return false;
}

In the case of an SMTP Exception, you can retrieve the actual status code and look up what the status means. 如果发生SMTP异常,则可以检索实际状态代码并查找状态的含义。 This would go in above your existing generic catch and handler. 这将超过您现有的通用捕获和处理程序。

catch (SmtpException sx) { // general SMTP errors
    var smtpStatus = sx.StatusCode;
    // See for values: https://msdn.microsoft.com/en-us/library/system.net.mail.smtpstatuscode(v=vs.110).aspx

    LogSmtpException(sx); // Log your exception review (you need to create)

    return false;
}
catch (Exception ex) { // general error NOT previously caught
    LogException(ex); // Log your exception review (you need to create)
    return false;
}

But there is an even more specific SMTP Exception you could get, usually for a bad address, so we could throw that on top of the 2 other catches 但是您可能会得到更具体的SMTP异常,通常是因为地址错误导致的,因此我们可以将其置于其他2个捕获之上

catch (SmtpFailedRecipientException rx) { // Bad recipient
    var smtpStatus = rx.StatusCode; // inherited from SmtpException

    // SEE: https://msdn.microsoft.com/en-us/library/system.net.mail.smtpfailedrecipientexception(v=vs.110).aspx

    LogSmtpException(sx); // Log your exception review (same as SmtpException)

    return false;
}


catch (SmtpException sx) { // general SMTP errors
    var smtpStatus = sx.StatusCode;
    LogSmtpException(sx);
    return false;
}

catch (Exception ex) { // general error NOT previously caught
    LogException(ex);
    return false;
}

What I have implemented 我已经实现了什么

My "Send Mail" routine is not a bool method; 我的“发送邮件”例程不是bool方法; it actually returns a KeyValuePair<int, string> , which is interpreted by the caller. 它实际上返回KeyValuePair<int, string> ,由调用方解释。

KeyValuePair<int, string> MessageStatus = new SendMail(my values);
if ((int)MessageStatus.Key == 250) {
    // all ok, proceed as normal 
} 
else {
    ViewData["StatusCode"] = MessageStatus.Key;
    ViewData["StatusMessage"] = MessageStatus.Value;
}

And my SendMail is structured like this. 我的SendMail的结构是这样的。 I do not lookup the exact reason at the time of the event, but I log the exceptions for later review. 事件发生时我没有查找确切原因,但是我记录了异常以供以后查看。

public KeyValuePair<int, string> SendMail (my values) {

    int StatusCode;
    string StatusMessage;

    // ===== standard message building code =====
    try {
        // ===== smtp client setup code =====

        smtp.Send(mail);
        StatusCode = 250;                      // Standard SMTP OK
        StatusMessage = "Message Sent";
    }
    catch (SmtpFailedRecipientException rx) {
        LogExceptionSMTP(rx, message.To);
        StatusCode = (int)rx.StatusCode;
        StatusMessage = rx.Message;
    }
    catch (SmtpException sx) {
        LogExceptionSMTP(sx, message.To);
        StatusCode = (int)sx.StatusCode;
        StatusMessage = sx.Message;
    }
    catch (Exception ex) {
        ex.Data.Add( "SendMail_Recipient", message.To );
        LogException(ex);
        StatusCode = 500;                      // Standard App Error Code
        StatusMessage = ex.Message;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM