Asp.Net Core MailKit: Remote certificate is invalid according to validation procedure

I have a question regarding sending email 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("myMail@gmail.com", "cjimenezber@gmail.com"));

        // From
        mailMsg.From.Add(new MailboxAddress("noreply@app.com", "Facturacion"));

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

        System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("myMail@gmail.com", "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've found in other posts related to it, this should be enough to send it using MailKit, however it doesn't work as expected. I am getting the following exception and I have no idea how to proceed from here.

This is an exception:

enter image description here

I saw this question, but I didn't make any sense to it: How to send an email using MailKit?

Any help is greatly appreciated, thanks.

+3


source to share


1 answer


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

This is most likely because the Google Root CA certificate was not imported into the root certificate store.

There are two ways:



  • Import Root CA certificate into your system (how to do it depending on Windows, Linux, MacOS)
  • Cancel certificate validation by setting your own certificate validation callback method to client.ServerCertificateValidationCallback

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

+4


source







All Articles