System.Net.Mail FAQ

Complete FAQ Listing


4.5 How do I send an email over SSL? Printer Friendly

Sending an email over Ssl is really simply with System.Net.Mail. In fact, all you need to do is set the .EnableSsl property on the SmtpClient property to true.

Below you will find an example of of SMTP over SSL.

Note: Due to what I believe is a bug in System.Net.Mail you *cannot* use SSL and Credentials (username/password) at the same time. I was never able to get that to work. I always got an exception from the remote mail server. I believe it has to do with the steps System.Net.Mail takes to initiate the SSL session and SMTP authentication.

[ C# ]

static void SSL()
{
//create the mail message
MailMessage mail = new MailMessage();

//set the addresses
mail.From = new MailAddress("me@mycompany.com");
mail.To.Add("you@yourcompany.com");

//set the content
mail.Subject = "This is an email";
mail.Body = "this is the body content of the email.";

//Port 587 is another SMTP port
SmtpClient smtp = new SmtpClient("127.0.01", 587);
smtp.EnableSsl = true;
smtp.Send(mail);
}



[ VB.NET ]
Sub SSL()
'create the mail message
Dim mail As New MailMessage()

'set the addresses
mail.From = New MailAddress("me@mycompany.com")
mail.To.Add("you@yourcompany.com")

'set the content
mail.Subject = "This is an email"
mail.Body = "this is the body content of the email."

'Port 587 is another SMTP port
Dim smtp As New SmtpClient("127.0.0.1", 587)
smtp.EnableSsl = True
smtp.Send(mail)
End Sub