在博問中(.net core怎么實現郵件發送)知道了MailKit無法使用阿里云郵件推送服務發送郵件的問題,自已實測也遇到同樣的問題,而用自己搭建的郵件服務器沒這個問題。
于是,向阿里云提交了工單。。。在提供了TCP抓包數據后,阿里云技術專員發現了問題所在:在認證通過后,MailKit發送了EHLO命令,然后才發送MAIL FROM命令,服務器在收到EHLO時會重置客戶端的認證狀態,所以后續的命令過來時,服務器認為客戶端沒有認證,于是報錯“503 Bad sequence of commands”。
知道了問題的原因后,首先想到的解決方法是不讓MailKit在發送MAIL FROM命令之前發送EHLO命令。
于是在github上簽出MailKit的源代碼,在 MailKit\Net\Smtp\SmtpClient.cs 中找到了下面的代碼:
if (host != "smtp.strato.de" && host != "smtp.sina.com") Ehlo (cancellationToken);
并且發現了對應這個問題的issue:EHLO after AUTH causes send failure with 5.7.0 even though authentication succeeded #162
原來這是已知問題,MailKit暫時采用了臨時解決方法,于是我們也依葫蘆畫瓢,加上了阿里云郵件推送服務的SMTP服務器:
if (host != "smtp.strato.de" && host != "smtp.sina.com" && !host.Equals("smtp.dm.aliyun.com", StringComparison.OrdinalIgnoreCase)) Ehlo (cancellationToken);
接著通過以下命令自己編譯出MailKit.dll(在 MailKit\bin\Debug\netstandard1.3 文件夾中)
git submodule update --init --recursive cd submodules\MimeKit\MimeKit dotnet restore cd ..\..\..\MailKit dotnet restore dotnet build
接著用這個 MailKit.dll 替換 .nuget\packages\MailKit\1.4.0\lib\netstandard1.3\MailKit.dll ,然后運行郵件發送程序,問題解決。
[更新1]
MailKit 的作者已經修復了這個問題,詳見 Added another broken SMTP server to the list
[更新2]
MailKit 1.4.1 已修復這個問題。
文章列表