简体   繁体   English

如何正确处理System.Net.Mail.SmtpException?

[英]How to correctly handle System.Net.Mail.SmtpException?

I have a simple smtpClient: 我有一个简单的smtpClient:

var smtp = new SmtpClient { Host = host, ...};
smtp.Send(message);

I can have a different host: smtp.gmail.com , smtp.yandex.ru etc. 我可以拥有一个不同的主人: smtp.gmail.comsmtp.yandex.ru等。

When smtp.Send(message); smtp.Send(message); executed I have different exception (depends on the host) due to the same problem - 2-factor verification is off. 执行我有不同的例外(取决于主机)由于同样的问题 - 双因素验证关闭。

For gmail its System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. 对于gmail,其System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

For yahoo and yandex its System.Net.Mail.SmtpException depth 0: The operation has timed out. (0x80131500) 对于yahoo和yandex,其System.Net.Mail.SmtpException depth 0: The operation has timed out. (0x80131500) System.Net.Mail.SmtpException depth 0: The operation has timed out. (0x80131500)

I don't now about others mail providers exception but how to correctly throw exception ("you need to enable 2-factor verification") just once? 我现在不介绍其他邮件提供程序异常但是如何正确抛出异常(“你需要启用双因素验证”)只需一次? Is it possible? 可能吗? Or how to minimize code duplication? 或者如何最小化代码重复?

I'm not sure how you are choosing which host to use (an if or switch statement?) but you could consider adding two new client classes which inherit from SmtpClient, eg for a YahooClient: 我不确定你是如何选择使用哪个主机( ifswitch语句?)但你可以考虑添加两个从SmtpClient继承的新客户端类,例如YahooClient:

class YahooClient : SmtpClient {

     private const string Host = "smtp.yahoo.com";

     Send(MailMessage message) { 

          /// Call base send and handle exception            
          try {
             base.Send(message)
          }
          catch(ex as SmtpException) {
              // Handle accordingly
          }
     }
}

Furthermore, you could introduce implement a suitable interface and use IoC (or strategy pattern etc.) to inject the correct client based on your configuration, eg 此外,您可以引入实现一个合适的接口,并使用IoC(或策略模式等)根据您的配置注入正确的客户端,例如

class YahooClient : SmtpClient, IMySmtpClient {

}

interface IMySmtpClient {
    void Send(MailMessage message);
}

class ConsumingMailSender(IMySmtpClient client) {

       // Create message and send
       var message = new MailMessage etc....

       client.Send(message);
}

This is perhaps overkill but could avoid violating SRP and having to perform conditional logic in the method you are currently using to send the email in. 这可能是矫枉过正,但可以避免违反SRP并且必须在您当前用于发送电子邮件的方法中执行条件逻辑。

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

相关问题 如何修复 System.Net.Mail.SmtpException - How Can I Fix System.Net.Mail.SmtpException 出现错误:-System.net.mail.SmtpException - Getting error:-System.net.mail.SmtpException System.Net.Mail.SmtpException:邮箱不可用 - System.Net.Mail.SmtpException: Mailbox unavailable system.net.mail.smtpException:无法连接到远程服务器? - system.net.mail.smtpException :Unable to connect to the remote server? System.Net.Mail.SmtpException:该地址具有无效的主机名 - System.Net.Mail.SmtpException: The address has an invalid host name System.Net.Mail.SmtpException {“发送电子邮件失败。”} - System.Net.Mail.SmtpException {“Failure Sending Email.”} System.Net.Mail.SmtpException:系统存储不足。 服务器响应如下:4.3.1系统资源不足 - System.Net.Mail.SmtpException: Insufficient system storage. The server response was: 4.3.1 Insufficient system resources 将白名单IP gmail smtp-System.Net.Mail.SmtpException:发送邮件失败 - Whitelist IP gmail smtp - System.Net.Mail.SmtpException: Failure sending mail System.Net.Mail.SmtpException:发送邮件失败。 无法从传输连接中读取数据 - System.Net.Mail.SmtpException: Failure sending mail. Unable to read data from the transport connection System.dll中发生了类型为'System.Net.Mail.SmtpException'的未处理的异常 - An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM