I am using MailKit
to try and send email though an exchange server via SMTP but when I try to connect I get the following error:
An exception of type 'System.Security.Authentication.AuthenticationException' occurred in MailKit.dll but was not handled in user code
Additional information: The remote certificate is invalid according to the validation procedure.
Googleing this brings up a lot of stuff about Gmail or as part of other error messages (like FTP or web api requests and such). I have talked with the IT guys and it is not a self-signed cert and we don't require authentication (in fact the BugZilla instance I admin is setup with the same settings and works fine). What am I doing wrong or how can I get more details to further troubleshoot?
using (var Client = new SmtpClient())
{
Client.Connect("mail.address.com", 587, false);
Client.AuthenticationMechanisms.Remove("XOAUTH2");
Client.Send(Message);
Client.Disconnect(true);
}
Edit: I have verified with IT that it is the same cert used in IIS that hosts the mail.address.com domain
as well as in exchange. I have also installed it as a trusted root but still get the same error.
Edit 2: If I update the code to Client.Connect("mail.address.com", 587, true);
then I get the error:
An exception of type 'System.IO.IOException' occurred in >System.Private.Networking.dll but was not handled in user code
Additional information: The handshake failed due to an unexpected packet format.
Try using Client.Connect("mail.address.com", 587, SecureSocketOptions.None);
to disable STARTTLS.
If you want to keep STARTTLS, you might try overriding Client.ServerCertificateValidationCallback
.
The easiest way to get more information about the error is to override ServerCertificateValidationCallback
with something like this:
bool ValidateRemoteCertificate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
Console.WriteLine ("SslPolicyErrors: {0}", sslPolicyErrors);
return sslPolicyErrors == SslPolicyErrors.None;
}