如何使用 C# 连接到支持 STARTTLS 的 SMTP 服务器并获取其 SSL 证书?我知道可以使用 openssl 来完成类似的事情
openssl s_client -starttls smtp -crlf -connect 192.168.0.1:25
我不想调用 openssl 并解析其输出。非常感谢如何使用 C# 完成此操作的示例。
一种方法是绑定到ServerCertificateValidationCallback回调并向服务器发送测试消息。邮件消息可能会失败,但证书仍然有效。
private void Form1_Load(System.Object sender, System.EventArgs e)
{
System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(RemoteServerCertificateValidationCallback);
using (System.Net.Mail.SmtpClient S = new System.Net.Mail.SmtpClient("smtp.gmail.com"))
{
S.EnableSsl = true;
using (System.Net.Mail.MailMessage M = new System.Net.Mail.MailMessage("[email protected]", "[email protected]", "Test", "Test"))
{
try
{
S.Send(M);
}
catch (Exception)
{
return;
}
}
}
}
private bool RemoteServerCertificateValidationCallback(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
Console.WriteLine(certificate);
return true;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)