上周使用 SmtpCliet 發送郵件測試,在服務端配置 SSL 465 / 993 情況 ,客戶端使用 465 SSL 端口發送郵件異常,測試代碼如下:
System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(RemoteServerCertificateValidationCallback); SmtpClient smtp = new SmtpClient(); smtp.EnableSsl = true; smtp.Port = 465; smtp.UseDefaultCredentials = false; smtp.DeliveryMethod = SmtpDeliveryMethod.Network;//指定電子郵件發送方式 smtp.Host =txtSMTP.Text ;//指定SMTP服務器 .....
=>如上代碼在 調用 smtp.send() 方法后 ,客戶端假死,郵件服務器日志顯示:
Performing SSL/TLS handshake for session 829. Verify certificate: False
=>將 smtp.EnableSsl 設為 false ,使用25端口,發送正常。 因此推斷,加密通道驗證有問題。 將郵件服務端 smtp:25 端口安全驗證規則改為: STARTTLS(Required) , smtp:465端口仍保持: SSL/TLS , 客戶端代碼如下 , 郵件發送成功。
System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(RemoteServerCertificateValidationCallback); SmtpClient smtp = new SmtpClient(); smtp.EnableSsl = true; smtp.UseDefaultCredentials = false; smtp.DeliveryMethod = SmtpDeliveryMethod.Network;//指定電子郵件發送方式 smtp.Host =txtSMTP.Text ;//指定SMTP服務器 .....
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; }
備注:
查閱MSDN , SmtpClient 版本只支持 Explicit SSL 客戶端鏈路過程如: Connect on 25 -> StartTLS (starts to encrypt) -> authenticate -> send data
http://cn.bing.com/search?q=c%23+mail+ssl&FORM=QSRE1
https://stackoverflow.com/questions/1011245/how-can-i-send-emails-through-ssl-smtp-with-the-net-framework
https://blogs.msdn.microsoft.com/webdav_101/2008/06/02/system-net-mail-with-ssl-to-authenticate-against-port-465/
https://msdn.microsoft.com/zh-cn/library/system.net.mail.smtpclient.enablessl(v=vs.110).aspx
--隱示SSL 示例:
http://blog.csdn.net/qxyywy/article/details/73962948