1
votes

I have a problem connecting to Office 365 using Imap and MailKit. The exception I get is The remote certificate is invalid according to the validation procedure

https://github.com/jstedfast/MailKit

Code:

imapClient = new ImapClient();

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

According to the documentation I can set the client to accept all certificates but I do not wan't to do this.

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

Microsoft Office 365 settings should be correct:

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

Has anyone else experienced this and solved it?

1

1 Answers

2
votes

I ended up running the following code and put a breakpoint in the method.

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

The certificate sent was the following:

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

After stopping Avast Email Protection everything started working again.