The remote certificate is invalid according to the validation procedure.
Hello. After updating MailKit to version 1.2.20 When I try to connect by using MailKit.ImapClient to gmail with default settings(Host = "imap.gmail.com", Port = 993, UseSSL = true), then the error appears "System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure." at MailKit.Net.Imap.ImapClient.Connect(String host, Int32 port, SecureSocketOptions options, CancellationToken cancellationToken). If I set SecureSocketOptions.None to connection settings, MailKit.ImapClient return Unexpectedly disconnected. What's wrong?
In MailKit prior to 1.2.20, I made the default certificate validation callback always return "valid", mostly to avoid this type of question for the multitude of mail servers that have self-signed certificates and I figured that anyone serious about security would override the ServerCertificateValidationCallback property on ImapClient. I've since had second thoughts about that because it opens people up to MITM attacks that they may not even be aware is a potential threat.
I don't think GMail's certificate is self-signed, but I have also noticed that on Mac and Linux, this now fails, but on Windows it seems to continue to work.
Are you on Mac or Linux?
Either way, the solution is to do something like this:
client.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => {
if (sslPolicyErrors == SslPolicyErrors.None)
return true;
// if there are errors in the certificate chain, look at each error to determine the cause.
if ((sslPolicyErrors & SslPolicyErrors.RemoteCertificateChainErrors) != 0) {
if (chain != null && chain.ChainStatus != null) {
foreach (var status in chain.ChainStatus) {
if ((certificate.Subject == certificate.Issuer) && (status.Status == X509ChainStatusFlags.UntrustedRoot)) {
// self-signed certificates with an untrusted root are valid.
continue;
} else if (status.Status != X509ChainStatusFlags.NoError) {
// if there are any other errors in the certificate chain, the certificate is invalid,
// so the method returns false.
return false;
}
}
}
// When processing reaches this line, the only errors in the certificate chain are
// untrusted root errors for self-signed certificates. These certificates are valid
// for default Exchange server installations, so return true.
return true;
}
return false;
};
I've just added this logic as a public static method called MailService.DefaultServerCertificateValidationCallback()
I've also made it the default callback for all SMTP, POP3 and IMAP client instances.
Hopefully this will not bite me in the butt :(
Thank you for information. It really helped for me.
One way to make this more secure is that you could get the Thumbprint of the X509Certificate you know is Google's GMail server and hardcode that into the callback so that you can compare the Thumbprint values.
if (certificate is X509Certificate2 && ((X509Certificate2) certificate).Thumbprint == "71DFBE124D89ED9218F539A82D4127FBB4BC9997")
return true;
Hi mate, I am getting this same error and I am on windows. No solution is working for me, I keep getting this same error the remote certificate is invalid according to the validation procedure. Any suggestions? Is it possible that there is something wrong to the server I am trying to connect? This is the server I am trying to connect actually mail.cosgroup.com at 143
You need to override http://www.mimekit.net/docs/html/P_MailKit_MailService_ServerCertificateValidationCallback.htm
I missed this change when you made it on March 3rd (I use my own validation function, so it doesn't affect me), and like you I tend to waffle on security checks like this. The more paranoid in me would prefer the more rigorous check, failing on self-signed certs (partly to raise awareness of the evilness of using self-signed certs for a public server like this, because MitM attacks), and if someone wants a less secure setting, they should override the more secure check.
That being said, I answer a lot of certificate questions ("Why doesn't your app connect to my self-signed-certificate-server?!"), and it gets tedious after a while, so it's hard to argue with your change.
Note that there's now a free way of getting actual (non-self-signed) certs: https://letsencrypt.org
Hey Jeffrey, Thanks for the suggestion, I was able to override and connect imap which is downloading the emails now but not sending them. No matter what I do, just keep getting this error
The SMTP server does not support authentication.
at MailKit.Net.Smtp.SmtpClient.Authenticate(Encoding encoding, ICredentials credentials, CancellationToken cancellationToken)
at MailKit.MailService.Authenticate(String userName, String password, CancellationToken cancellationToken)
I will be very glad if you could help?
Further to this, if I check Capabilities, I get this error while sending emails
Requested action not taken: mailbox unavailable
at MailKit.Net.Smtp.SmtpClient.ProcessRcptToResponse(SmtpResponse response, MailboxAddress mailbox)
at MailKit.Net.Smtp.SmtpClient.RcptTo(MimeMessage message, MailboxAddress mailbox, CancellationToken cancellationToken)
at MailKit.Net.Smtp.SmtpClient.Send(FormatOptions options, MimeMessage message, MailboxAddress sender, IList`1 recipients, CancellationToken cancellationToken, ITransferProgress progress)
at MailKit.Net.Smtp.SmtpClient.Send(FormatOptions options, MimeMessage message, CancellationToken cancellationToken, ITransferProgress progress)
at MailKit.MailTransport.Send(MimeMessage message, CancellationToken cancellationToken, ITransferProgress progress)
And on capability check, server has SmtpCapabilities.EightBitMime and size restriction of 20971520
The SMTP server does not support authentication.
at MailKit.Net.Smtp.SmtpClient.Authenticate(Encoding encoding, ICredentials credentials, CancellationToken cancellationToken)
at MailKit.MailService.Authenticate(String userName, String password, CancellationToken cancellationToken)
This error means that your SMTP server does not support authentication, so... just don't authenticate (i.e. skip calling the client.Authenticate(...) method).
Requested action not taken: mailbox unavailable
at MailKit.Net.Smtp.SmtpClient.ProcessRcptToResponse(SmtpResponse response, MailboxAddress mailbox)
at MailKit.Net.Smtp.SmtpClient.RcptTo(MimeMessage message, MailboxAddress mailbox, CancellationToken cancellationToken)
at MailKit.Net.Smtp.SmtpClient.Send(FormatOptions options, MimeMessage message, MailboxAddress sender, IList`1 recipients, CancellationToken cancellationToken, ITransferProgress progress)
at MailKit.Net.Smtp.SmtpClient.Send(FormatOptions options, MimeMessage message, CancellationToken cancellationToken, ITransferProgress progress)
at MailKit.MailTransport.Send(MimeMessage message, CancellationToken cancellationToken, ITransferProgress progress)
This error means that one of the email addresses (aka mailboxes) that you are sending the message to does not exist.
Hey Jeff, if I skip authenticate it throws this mailbox error and its just a normal valid hotmail email. Stuck
The error is from the server, so I'd be fairly confident that the error is accurate. Maybe the mailbox is full?
No, I have tried sending to different emails, same error, and I have tried a different email from the same server but same result. I don't understand, the same email works with some systems, do you think I should try sending by .net SMTP class? The client is saying he uses owa the online outlook system and the email account works fine with that.
Sure, give that a try. You could also try getting a log (see the FAQ) to see what capabilities the server supports.
normal valid hotmail email: Note that hotmail also support exchange, and most clients are more than likely using exchange and not imap.
That being said:
SMTP:
$ telnet smtp-mail.outlook.com 587
Trying 65.55.163.152...
Connected to smtp.glbdns2.microsoft.com.
Escape character is '^]'.
220 BLU437-SMTP99.smtp.hotmail.com Microsoft ESMTP MAIL Service, Version: 8.0.9200.16384 ready at Tue, 17 May 2016 15:30:32 -0700
EHLO hotmail.com
250-BLU437-SMTP99.smtp.hotmail.com Hello [XX.XXX.XXX.XX]
250-TURN
250-SIZE 41943040
250-ETRN
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-8bitmime
250-BINARYMIME
250-CHUNKING
250-VRFY
250-TLS
250-STARTTLS
250 OK
IMAP:
$ openssl s_client -connect imap-mail.outlook.com:993 -crlf -quiet
depth=1 C = BE, O = GlobalSign nv-sa, CN = GlobalSign Organization Validation CA - SHA256 - G2
verify error:num=20:unable to get local issuer certificate
* OK Outlook.com IMAP4rev1 server version 17.4.0.0 ready (BAY451-IMAP42)
A0001 CAPABILITY
* CAPABILITY IMAP4rev1 CHILDREN ID NAMESPACE UIDPLUS UNSELECT AUTH=PLAIN AUTH=XOAUTH2 SASL-IR
A0001 OK CAPABILITY completed
@atiqi36 make sure to connect using SecureSocketOptions.StartTls, this might then expose what authentication options Hotmail supports for SMTP. Once you do that, you'll probably need to add back the call to Authenticate().
Guys, I think I may have been unclear. It's not the hotmail server I am using. The smtp server I am using to send email is mail.cosgroup.com and the email is of the same domain like [email protected] I can't send email at all, I have tried all the configuration, Auto, SSL, TLS etc but nothing works. Tried with authentication or without but still don't work. And on capability check, server has SmtpCapabilities.EightBitMime and size restriction of 20971520 I can connect to imap to download emails so you can rule out the firewall. What could be wrong? What ever I do I get this bloody error "The SMTP server does not support authentication."
The server might not support authentication until you have toggled into TLS mode.
So what you need to do is connect on port 25 or 587 (which ever one you are using) and pass SecureSocketOptions.StartTls:
using (var client = new SmtpClient (new ProtocolLogger ("smtp.log"))) {
client.Connect ("mail.cosgroup.com", 587, SecureSocketOptions.StartTls);
client.Authenticate ("username", "password");
client.Send (message);
client.Disconnect (true);
}
Now getting this error "The SMTP server does not support the STARTTLS extension." :(
Have you tried contacting your network administrator and asking him/her what settings to use for connecting via SMTP?