偶然興起,想做一個(gè)后臺(tái)監(jiān)控PLC狀態(tài)的服務(wù)。功能如下:監(jiān)控到PLC狀態(tài)值異常后觸發(fā)郵件推送,狀態(tài)改變后只推送一次。開(kāi)始使用的是.net6.0開(kāi)發(fā)框架開(kāi)發(fā),一切都很順利,郵件也能正常推送。但由于現(xiàn)場(chǎng)工控機(jī)系統(tǒng)不是WIN10 20H2的最新版本,導(dǎo)致系統(tǒng)未安裝.Net6.0 Runtime。而我也沒(méi)有再去安裝的打算。我重新使用了.net FrameWork4.7 框架進(jìn)行開(kāi)發(fā)。開(kāi)發(fā)完成后,我以為能正常運(yùn)行。但出現(xiàn)了不可預(yù)知的錯(cuò)誤——服務(wù)器響應(yīng):5.7.1 Client was not authenticated。下面分別是2個(gè)框架下發(fā)送郵件的代碼:
.Net 6.0框架:
點(diǎn)擊查看代碼
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 框架:
點(diǎn)擊查看代碼
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存在問(wèn)題。經(jīng)過(guò)斷點(diǎn)調(diào)試,終于發(fā)現(xiàn)了2個(gè)環(huán)境發(fā)送郵件時(shí)存在的差異。
.Net 6.0框架下用戶傳入的憑證(賬號(hào)密碼)SMTP服務(wù)器可正常獲取到
.Net FrameWork 框架下竟然獲取的憑證為空
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-776950.html
經(jīng)過(guò)短暫思考,我決定修改下.Net FrameWork框架下的開(kāi)發(fā)代碼。如下:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-776950.html
點(diǎn)擊查看代碼
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;
}
}
點(diǎn)擊查看代碼
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);
}
}
點(diǎn)擊查看代碼
//
// 摘要:
// Gets or sets a System.Boolean value that controls whether the System.Net.CredentialCache.DefaultCredentials
// are sent with requests.
//
// 返回結(jié)果:
// 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;
}
到了這里,關(guān)于.Net FrameWork 框架下使用System.Net.Mail封裝類 發(fā)送郵件失敗:服務(wù)器響應(yīng):5.7.1 Client was not authenticated 解決方案的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!