.Net FrameWork 框架下使用System.Net.Mail封装类 发送邮件失败:服务器响应:5.7.1 Client was not authenticated 解决方案

  • .Net FrameWork 框架下使用System.Net.Mail封装类 发送邮件失败:服务器响应:5.7.1 Client was not authenticated 解决方案已关闭评论
  • 82 次浏览
  • A+
所属分类:.NET技术
摘要

偶然兴起,想做一个后台监控PLC状态的服务。功能如下:监控到PLC状态值异常后触发邮件推送,状态改变后只推送一次。开始使用的是.net6.0开发框架开发,一切都很顺利,邮件也能正常推送。但由于现场工控机系统不是WIN10 20H2的最新版本,导致系统未安装.Net6.0 Runtime。而我也没有再去安装的打算。我重新使用了.net FrameWork4.7 框架进行开发。开发完成后,我以为能正常运行。但出现了不可预知的错误——服务器响应:5.7.1 Client was not authenticated。下面分别是2个框架下发送邮件的代码:
.Net 6.0框架:

偶然兴起,想做一个后台监控PLC状态的服务。功能如下:监控到PLC状态值异常后触发邮件推送,状态改变后只推送一次。开始使用的是.net6.0开发框架开发,一切都很顺利,邮件也能正常推送。但由于现场工控机系统不是WIN10 20H2的最新版本,导致系统未安装.Net6.0 Runtime。而我也没有再去安装的打算。我重新使用了.net FrameWork4.7 框架进行开发。开发完成后,我以为能正常运行。但出现了不可预知的错误——服务器响应:5.7.1 Client was not authenticated。下面分别是2个框架下发送邮件的代码:
.Net 6.0框架:

点击查看代码
public bool Send() {     try     {         SmtpClient smtp = new SmtpClient(this.Host!, (int)this.Port!) { Credentials = new NetworkCredential(this.User!, this.Password!), EnableSsl = true, UseDefaultCredentials = false };         MailMessage message = new MailMessage(this.SenderAddress!, this.ReciverAddress!, this.Subject, this.Body) { From = new MailAddress(this.SenderAddress!, this.SenderName) };         ServicePointManager.ServerCertificateValidationCallback = delegate (object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)         {             return true;         };         smtp.Send(message);         return true;     }     catch (Exception)     {         return false;     } } 

.Net FrameWork 4.7 框架:

点击查看代码
public bool Send() {     try     {         SmtpClient smtp = new SmtpClient(this.Host!, (int)this.Port!) { Credentials = new NetworkCredential(this.User!, this.Password!), EnableSsl = true, UseDefaultCredentials = false };         MailMessage message = new MailMessage(this.SenderAddress!, this.ReciverAddress!, this.Subject, this.Body) { From = new MailAddress(this.SenderAddress!, this.SenderName) };         ServicePointManager.ServerCertificateValidationCallback = delegate (object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)         {             return true;         };         smtp.Send(message);         return true;     }     catch (Exception)     {         return false;     } } 

在不同的开发框架下,使用的代码完全一致。但是在.Net FrameWork 4.7 框架下发送邮件才会出现异常:**5.7.1 Client was not authenticated**,而.Net 6.0 环境下不会。

我不得不怀疑是不是微软的封装类System.Net.Mail存在问题。经过断点调试,终于发现了2个环境发送邮件时存在的差异。
.Net 6.0框架下用户传入的凭证(账号密码)SMTP服务器可正常获取到

.Net FrameWork 框架下使用System.Net.Mail封装类 发送邮件失败:服务器响应:5.7.1 Client was not authenticated 解决方案
.Net FrameWork 框架下竟然获取的凭证为空

.Net FrameWork 框架下使用System.Net.Mail封装类 发送邮件失败:服务器响应:5.7.1 Client was not authenticated 解决方案

经过短暂思考,我决定修改下.Net FrameWork框架下的开发代码。如下:

点击查看代码
public bool Send() {     try     {         smtp = new SmtpClient(this.Host, (int)this.Port);         smtp.Credentials = new NetworkCredential(this.User,this.Password);         smtp.EnableSsl = true;         //smtp.UseDefaultCredentials = false;         MailMessage message = new MailMessage(this.SenderAddress, this.ReciverAddress, this.Subject, this.Body) { From = new MailAddress(this.SenderAddress, this.SenderName) };         ServicePointManager.ServerCertificateValidationCallback = delegate (object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)         {             return true;         };         smtp.Send(message);         return true;     }     catch (Exception)     {         return false;     } } 

上述代码中,取消了UseDefaultCredentials属性的使用。运行后,邮件发送正常。 那么问题来了,为什么使用了UseDefaultCredentials属性就会导致Credentials 凭证为空呢?看下.Net FrameWork框架System.Net.Mail源码:

点击查看代码
public bool UseDefaultCredentials {     get     {         if (!(transport.Credentials is SystemNetworkCredential))         {             return false;         }          return true;     }     set     {         if (InCall)         {             throw new InvalidOperationException(SR.GetString("SmtpInvalidOperationDuringSend"));         }          transport.Credentials = (value ? CredentialCache.DefaultNetworkCredentials : null);     } } 

再对比下.net6.0 框架System.Net.Mail源码:

点击查看代码
 //  // 摘要:  //     Gets or sets a System.Boolean value that controls whether the System.Net.CredentialCache.DefaultCredentials  //     are sent with requests.  //  // 返回结果:  //     true if the default credentials are used; otherwise false. The default value  //     is false.  //  // 异常:  //   T:System.InvalidOperationException:  //     You cannot change the value of this property when an email is being sent.  public bool UseDefaultCredentials  {      get;      set;  } 

水落石出,微软误我啊!?