上周使用 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

 

 posted on 2018-03-19 10:05  仰光  阅读(3828)  评论(0编辑  收藏  举报