偶然兴起,想做一个后台监控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 框架下竟然获取的凭证为空
文章来源:https://www.toymoban.com/news/detail-776950.html
经过短暂思考,我决定修改下.Net FrameWork框架下的开发代码。如下:文章来源地址https://www.toymoban.com/news/detail-776950.html
点击查看代码
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;
}
到了这里,关于.Net FrameWork 框架下使用System.Net.Mail封装类 发送邮件失败:服务器响应:5.7.1 Client was not authenticated 解决方案的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!