Use TLS/SSL with SMTP in .NET
Mail.dll SMTP .NET email component supports Secure Socket Layer (SSL) and Transport Layer Security (TLS) protocols to authenticate the server and secure client-server email sending.
There are two modes in which Mail.dll can work:
- Implicit – where Mail.dll SMTP client immediately connects using secure channel,
- Explicit – where Mail.dll SMTP client connects on unsecured channel first and then secures the communication by issuing STARTTLS command. This mode is sometimes called TLS.
In both cases, by default, Secure Sockets Layer (SSL) 3.0 and Transport Layer Security (TLS) 1.0, 1.1, 1.2, 1.3 are acceptable for secure communication. You can change the defaults using Smtp.SSLConfiguration property.
Smtp client may decide to secure the channel, if SMTP server explicitly forbids logging-in on unsecured channel and you are using UseBestLogin method.
SMTP implicit TLS/SSL mode
Mail.dll SMTP component connects using secure TLS/SSL channel. You need to know in advance if, the server supports TLS/SSL connections – ask your administrator. Typically, SMTP over TLS/SSL is associated with port 465, but this is not always the case. You can always specify different, then standard port, using ConnectSSL method overload.
// C# version
using Limilabs.Mail;
using Limilabs.Client.POP3;
class Program
{
static void Main(string[] args)
{
using (Smtp smtp = new Smtp())
{
smtp.ConnectSSL("mail.example.com");
smtp.UseBestLogin("user", "password");
MailBuilder builder = new MailBuilder();
builder.Text = "text";
builder.From.Add(new MailBox("from@example.com"));
builder.To.Add(new MailBox("to@example.com"));
IMail email = builder.Create();
smtp.SendMessage(email);
smtp.Close();
}
}
};
' VB.NET version
Imports Limilabs.Mail
Imports Limilabs.Client.SMTP
Public Module Module1
Public Sub Main(ByVal args As String())
Using smtp As New Smtp()
smtp.ConnectSSL("mail.example.com")
smtp.UseBestLogin("user", "password")
Dim builder As New MailBuilder()
builder.Text = "text"
builder.From.Add(New MailBox("from@example.com"))
builder.[To].Add(New MailBox("to@example.com"))
Dim email As IMail = builder.Create()
smtp.SendMessage(email)
smtp.Close()
End Using
End Sub
End Module
SMTP explicit TLS/SSL mode
Mail.dll SMTP component connects using clear text channel and secures the channel using TLS/SSL by issuing STARTTLS command. Typically standard SMTP ports: 25 or 587 are used, but this is not always the case. You can always specify different then standard port using Connect method overloads. By default 587 port is used.
// C# version
using Limilabs.Mail;
using Limilabs.Mail.Headers;
using Limilabs.Client.SMTP;
class Program
{
static void Main(string[] args)
{
using (Smtp smtp = new Smtp())
{
smtp.Connect("mail.example.com");
smtp.StartTLS();
smtp.UseBestLogin("user", "password");
MailBuilder builder = new MailBuilder();
builder.Text = "text";
builder.From.Add(new MailBox("from@example.com"));
builder.To.Add(new MailBox("to@example.com"));
IMail email = builder.Create();
smtp.SendMessage(email);
smtp.Close();
}
}
};
' VB.NET version
Imports Limilabs.Mail
Imports Limilabs.Mail.Headers
Imports Limilabs.Client.SMTP
Public Module Module1
Public Sub Main(ByVal args As String())
Using smtp As New Smtp()
smtp.Connect("mail.example.com")
smtp.StartTLS()
smtp.UseBestLogin("user", "password")
Dim builder As New MailBuilder()
builder.Text = "text"
builder.From.Add(New MailBox("from@example.com"))
builder.[To].Add(New MailBox("to@example.com"))
Dim email As IMail = builder.Create()
smtp.SendMessage(email)
smtp.Close()
End Using
End Sub
End Module
After you connect, you can check, if your SMTP server supports explicit TLS/SSL using following code:
// C# version
bool supportsStartTLS = smtp.SupportedExtensions()
.Contains(SmtpExtension.StartTLS);
' VB.NET version
Dim supportsStartTLS As Boolean = smtp.SupportedExtensions() _
.Contains(SmtpExtension.StartTLS)
You can read more here on how to know which extensions does your server support.
Self-signed certificates
If you are using self-signed certificates you may encounter this error: The remote certificate is invalid according to the validation procedure.
Get Mail.dll
March 4th, 2015 at 15:53
How i can get this class named Limilabs,
without this it is not usefull for any body
March 6th, 2015 at 08:58
@leo
You can download Mail.dll here:
http://www.limilabs.com/mail
October 3rd, 2015 at 11:20
something is missing: ERROR: Send hello first
means do a hello before sending STARTTLS
October 4th, 2015 at 15:28
@Hendrik
You are right smtp.Ehlo() is missing from the examples (after Connect and after StartTLS methods).The latest version will not require those.
Edit:
Helo/Ehlo commands are no longer needed in the latest version.
October 25th, 2015 at 16:31
I recently purchased your product and have successfully used it to read emails. Today, I started SMTP tests and am getting an error when I attempt to use the ConnectSSL method that accepts a port #. Also, I get an error if I try to call the StartTLS method. All my test use a Gmail account.
ConnectSSL error:
The handshake failed due to an unexpected packet format.
StartTLS error:
5.5.1 Unrecognized command. u1sm39033912yhu.11
Thanks
October 25th, 2015 at 17:40
@Tim
You need to specify correct port to use SSL. From SMTP protocol it’s usually 465. You can also use Smtp.DefaultSSLPort property.
To use StartTLS method first connect without SSL (otherwise you’ll get “Unrecognized command” error).
You can check if server supports StartTLS command with following code:
November 5th, 2015 at 10:12
[…] Use SSL with SMTP […]