0

How can I bypass smtp server certificate validation in .Net Core 2.2?

using System;
using System.Net;
using System.Net.Mail;

namespace EmailTest
{
    internal static class Program
    {
        static void Main(string[] args)
        {
            SmtpClient client = new SmtpClient("192.168.0.10", 587);

            client.EnableSsl = true;
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential("username", "p4ssw0rd");

            MailMessage mailMessage = new MailMessage();
            mailMessage.From = new MailAddress("username@example.com");
            mailMessage.To.Add("customer@example.com");
            mailMessage.Body = "test email from dotnet core";
            mailMessage.Subject = "subject";
            client.Send(mailMessage);
        }
    }
}

I already tried

System.Net.ServicePointManager.ServerCertificateValidationCallback = (x, y, z, t) => true;

Which had no effect.

I also have already looked at the answers to this question, they all are about System.Net.Http.HttpClient. I am asking whether there is a way to bypass certificate validation for System.Net.Mail.SmtpClient.

  • 1
    Does this answer your question? [bypass invalid SSL certificate in .net core](https://stackoverflow.com/questions/38138952/bypass-invalid-ssl-certificate-in-net-core) – Sani Huttunen Nov 04 '19 at 14:21
  • 2
    `We don't recommend that you use the SmtpClient class for new development because SmtpClient doesn't support many modern protocols. Use MailKit or other libraries instead. For more information, see SmtpClient shouldn't be used on GitHub.` [See source](https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient?view=netcore-2.2#remarks). – Sani Huttunen Nov 04 '19 at 14:50

0 Answers0