前段時間看文章瞭解到發郵件的SmtpClient已通過時了,微軟官方推薦你們用其餘解決方案,例如MailKit。git
https://docs.microsoft.com/zh-cn/dotnet/api/system.net.mail.smtpclient?view=netcore-2.2github
SmtpClient Class 定義 命名空間: System.Net.Mail Assemblies: System.dll, netstandard.dll, System.Net.Mail.dll 警告 此 API 現已過期。 容許應用程序使用簡單郵件傳輸協議 (SMTP) 來發送電子郵件。 C# 複製 [System.Obsolete("SmtpClient and its network of types are poorly designed, we strongly recommend you use https://github.com/jstedfast/MailKit and https://github.com/jstedfast/MimeKit instead")] public class SmtpClient : IDisposable
關於MailKit的使用,網上大把,垂手可得就搞定了,本機調試沒問題,可是把服務端軟件發佈到Linux Docker運行,報錯了api
An error occurred while attempting to establish an SSL or TLS connection. One possibility is that you are trying to connect to a port which does not support SSL/TLS. The other possibility is that 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. at MailKit.Net.Smtp.SmtpClient.ConnectAsync(String host, Int32 port, SecureSocketOptions options, Boolean doAsync, CancellationToken cancellationToken)
報錯信息裏有一個鏈接,bash
https://github.com/jstedfast/MailKit/blob/master/FAQ.md#InvalidSslCertificate服務器
查看介紹,已經按照要求去作了。微信
我用的是騰訊企業郵箱,Asp.Net Core服務器部署在阿里雲,開始還覺得兩家的服務器不相容,後來發如今VMWare安裝的CentOS虛擬機上測試一樣報錯,肯定是Linux環境下的問題。因而開啓百度模式,網上有不少說法,主要有幾大類:測試
一,把client.ConnectAsync(「smtp.exmail.qq.com」, 465, SecureSocketOptions.Auto)鏈接郵件服務器的參數換一下ui
我把SecureSocketOptions的全部選項都換了一遍,都報錯。this
二,把鏈接郵件服務器端口從465改成587阿里雲
改了仍然報錯。
三,把賬號密碼改成客戶端專用密碼
這個其實要先用微信綁定郵箱,而後,之前的登陸密碼做廢了,須要使用客戶端專用密碼,否則連Foxmail客戶端都沒法收郵件了。可是我用了客戶端專用密碼,仍是報錯。
在stackoverflow上面看到有人遇到了一樣的問題。
https://stackoverflow.com/questions/55013298/cryptographicexception-exception-when-setting-up-ssl-handshake-with-mailkit-usin
可是沒有解決方案!做者把問題提交給微軟了。
這麼簡單的一個功能,竟然還有這麼大的一個坑!真是蛋疼……
走投無路之際,到做者提交的這個問題下面看一看,
https://github.com/Microsoft/dotnet/issues/973
竟然有一個湖南的同窗也在說遇到了這個問題,怎麼辦?
做者的答覆是:No - sorry we haven't solved this. We worked around this by disabling the revocation check (property on the Malkit client).
忽然看到了一線但願!能夠禁用一個什麼鬼屬性,把這個問題繞過去。
在SmtpClient的屬性裏翻了一下,恩,應該就是它了,
client.CheckCertificateRevocation = false;
設置爲false,測試一下,真的能夠發郵件了!最後代碼長這樣
using (var client = new SmtpClient()) { client.CheckCertificateRevocation = false; // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS) client.ServerCertificateValidationCallback = (s, c, h, e) => true; await client.ConnectAsync(「smtp.exmail.qq.com」, 465, SecureSocketOptions.Auto); // Note: only needed if the SMTP server requires authentication //若是騰訊企業郵箱綁定了微信,須要把密碼改成客戶端專用密碼 await client.AuthenticateAsync(Username, Password); await client.SendAsync(message); await client.DisconnectAsync(true); }
順帶說一下,採用SecureSocketOptions.Auto參數,若是鏈接465端口,自動轉換爲SslOnConnect,若是鏈接587端口,自動轉換爲StartTlsWhenAvailable