• SmtpClient SSL 发送邮件异常排查


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

  • 相关阅读:
    哪种写法更好?<script></script> vs/or <script type=”text/javasript”></script>
    JS 脚本应该放在页面哪个位置 head body foot
    List<T> ForEach break
    嵌套JSON 取出name与value
    C# 改变图片尺寸(压缩),Image Resize
    tornado
    appachebench网站压力测试
    mysql分区分表
    redis的持久化存储,RDB与AOF
    MEMCACHE的内存管理和删除策略
  • 原文地址:https://www.cnblogs.com/a_bu/p/8599989.html
Copyright © 2020-2023  润新知