Getting SmtpClient to work with a self-signed SSL certificate

I am trying to use the System.Net.Mail.SmtpClient class to send email through my company mail server. All SMTP connections to the mail server must be SSL, and it uses a self-signed certificate. This is good for Outlook, where you can just click ok in the warning dialog, but does anyone know a way to force SmtpClient to accept a self-signed certificate?

I plan to use this application on the Windows Azure platform, so I cannot install the signed certificate itself as a trusted root.

+7
c # ssl smtpclient self-signed
source share
2 answers

My problem ended up being that the .Net class SmtpClient does not seem to support using port 465 for SMTP SSL connections. Using port 25 with a self-signed SSL certificate worked correctly.

Question in MSDN System.Net Can SmtpClient be configured to work with a self-signed certificate? .

+3
source share

You can take a look at the ServerCertificateValidationCallback property:

ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true; 

It is a callback that is called by the runtime when it tries to verify the SSL certificate. By returning true , you basically say that you don't care whether the certificate is valid or not → you always accept it. Of course, self-signed certificates in a production environment are not a good idea.

+9
source share

All Articles