.Net FrameWork 框架下使用System.Net.Mail封装类 发送邮件失败:服务器响应:5.7.1 Client was not authenticated 解决方案
偶然兴起,想做一个后台监控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;
}
}
我不得不怀疑是不是微软的封装类System.Net.Mail存在问题。经过断点调试,终于发现了2个环境发送邮件时存在的差异。
.Net 6.0框架下用户传入的凭证(账号密码)SMTP服务器可正常获取到
.Net FrameWork 框架下竟然获取的凭证为空
经过短暂思考,我决定修改下.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;
}
}
点击查看代码
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);
}
}
点击查看代码
//
// 摘要:
// 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;
}