.net 使用MimeKit与MailKit发送邮件出现 An error occurred while attempting to establish an SSL or TLS connection.(转)

现象

服务正式上线半年多,无错误。突然有一天发现发送邮件失败,日志系统打印出日志如下:

邮件发送失败:An error occurred while attempting to establish an SSL or TLS connection.

The SSL certificate presented by the server is not trusted by the system for one or more of the following reasons:
1. The server is using a self-signed certificate which cannot be verified.
2. The local system is missing a Root or Intermediate certificate needed to verify the server's certificate.
3. The certificate presented by the server is expired or invalid.

See https://github.com/jstedfast/MailKit/blob/master/FAQ.md#InvalidSslCertificate for possible solutions.

原因

查看官方FAQ https://github.com/jstedfast/MailKit/blob/master/FAQ.md

发现我使用的是25端口,是一个标准纯文本端口(这些端口根本不支持任何SSL / TLS加密),我去看我的代码也没有使用ssl,感觉我的代码没有什么问题,而且已经在线上正常使用半年了;
根据现象明显确定是Connect 方法报错,然后去看官方文档
http://www.mimekit.net/docs/html/M_MailKit_MailService_Connect_3.htm

官方文档 该方法后面提示:

useSsl参数仅控制客户端是否进行ssl包装的连接。换句话说,即使useSsl参数为false,如果邮件服务器支持STARTTLS扩展,仍然可以使用SSL/TLS。
若要禁用所有SSL/TLS的使用,请使用带有SecureSocketOptions值的Connect(String、Int32、SecureSocketOptions、CancellationToken)重载。

原因找到了,邮件服务器开启了ssl支持,我使用的这个方法,usesll=false,任然去使用ssl/tls。而25端口不支持ssl/tls,所以连接不上;最后使用Connect(String、Int32、SecureSocketOptions、CancellationToken)重载就可以了

原文地址:https://www.cnblogs.com/25miao/p/13354072.html