MailKit C# ImapClient.Connect() 到 Office 365 生成异常:"The remote certificate is invalid according to the validation procedure"

MailKit C# ImapClient.Connect() to Office 365 generating exception: "The remote certificate is invalid according to the validation procedure"

我在使用 Imap 和 MailKit 连接到 Office 365 时遇到问题。我得到的例外是 The remote certificate is invalid according to the validation procedure

https://github.com/jstedfast/MailKit

代码:

imapClient = new ImapClient();

imapClient.Connect("outlook.office365.com", 993, true);

根据文档,我可以将客户端设置为接受所有证书,但我不想这样做。

// For demo-purposes, accept all SSL certificates
imapClient.ServerCertificateValidationCallback = (s,c,h,e) => true;

Microsoft Office 365 设置应该正确:

https://support.office.com/en-us/article/POP-and-IMAP-settings-for-Outlook-Office-365-for-business-7fc677eb-2491-4cbc-8153-8e7113525f6c

有没有其他人遇到并解决了这个问题?

我结束了 运行 以下代码并在方法中放置了一个断点。

ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
    return true;
};

发送的证书如下:

certificate.Issuer
"CN=avast! Web/Mail Shield Root, O=avast! Web/Mail Shield, OU=generated by avast! antivirus for SSL/TLS scanning"

停止 Avast Email Protection 后,一切又开始工作了。