views:

356

answers:

3

I'm attempting to use the System.Net.Mail.SmtpClient class to relay an email through my company's email server. All SMTP connections to the mail server have to be SSL and it uses a self signed certificate. That's fine for Outlook where you can just click ok on the warning dialogue but does anyone know a way to get SmtpClient to accept a self signed certificate?

I'm planning on using this app on the Windows Azure Platform so I won't be able to install the self signed certificate as a trusted root.

+1  A: 

You may take a look at the ServerCertificateValidationCallback property:

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

It represents a callback which is called by the runtime when it tries to validate an SSL certificate. By returning true you basically say that you don't care if the certificate is valid or not -> you always accept it. Of course having self signed certificates in production environment is not a good idea.

Darin Dimitrov
I did look into the ServicePoint angle a little bit but I saw this comment on the ServicePoint/Members doc page "Provides connection management for HTTP connections" so I get the impression it won't be used for SMTP. Plus when I try it and put a log message in the callback it's never called.
sipwiz
You need to put this **before** making any attempts to instantiate the `SmtpClient` class, ideally in some global initialization section of your application.
Darin Dimitrov
+1  A: 

My issue ended up being that the .Net SmtpClient class apparently doesn't support the use of port 465 for SMTP SSL connections. Using port 25 with a self signed SSL certificate worked correctly.

MSDN System.Net forum question Can SmtpClient be configured to work with a self signed certificate?.

sipwiz
A: 

Darin Dimitrov's answer is correct.

chrisbhmg