Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net Core MailKit: The remote certificate is invalid according to the validation proceedure

I have a question regarding email sending on asp.net core.

So I have this code:

private void SendEmailLocalSMTP(string email, string subject, string message)
{
        MimeMessage mailMsg = new MimeMessage();

        //TESTING ENVIRONMENT ONLY!!!
        mailMsg.To.Add(new MailboxAddress("[email protected]", "[email protected]"));

        // From
        mailMsg.From.Add(new MailboxAddress("[email protected]", "Facturacion"));

        // Subject and multipart/alternative Body
        mailMsg.Subject = subject;
        string html = message;

        System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("[email protected]", "myPassword");

        // Init SmtpClient and send
        using (var client = new SmtpClient())
        {
            client.Connect("smtp.gmail.com", 587, SecureSocketOptions.StartTls);
            client.AuthenticationMechanisms.Remove("XOAUTH2");
            client.Authenticate(credentials);
            client.Send(mailMsg);
            client.Disconnect(true);
        }
    }

From what I have found in other posts somewhat related, this should be enough to send it using MailKit, however it's not working properly. I am getting the following exception and I don't know how to proceed from here.

This is the exception:

enter image description here

I've seen this question, but I haven't made much sense from it: How to send email by using MailKit?

Any help is highly appreciated, thanks.

like image 702
Carlos Jimenez Bermudez Avatar asked Apr 23 '17 02:04

Carlos Jimenez Bermudez


People also ask

What is a remote certificate?

Remote certificates—These remote certificates are public certificates without private keys. They can be deleted, imported, and downloaded, and their details can be viewed in the same way as local certificates.

What is not available Error The remote certificate is invalid according to the validation procedure?

This usually occurs because either of the following are true: The certificate is self-signed and not added as a trusted certificate. The certificate is expired. The certificate is signed by a root certificate that's not installed on your machine.


1 Answers

The problem you are hitting is that your system does not trust the GMail SSL certificate.

Most likely the reason for this is because the Google Root CA certificate has not been imported into your Root certificate store.

There are two ways around this:

  1. Import Google's Root CA certificate into your system (how to do this varies depending on Windows, Linux, MacOS)
  2. Override the certificate validation by setting your own certificate validation callback method on client.ServerCertificateValidationCallback

For more information, see the FAQ: https://github.com/jstedfast/MailKit/blob/master/FAQ.md#InvalidSslCertificate

like image 165
jstedfast Avatar answered Sep 28 '22 02:09

jstedfast