現象
服務正式上線半年多,無錯誤。突然有一天發現發送郵件失敗,日志系統打印出日志如下:
郵件發送失敗: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)重載就可以了