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.

Here you can find more details on SSL vs TLS vs STARTTLS.

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

Tags:      

Questions?

Consider using our Q&A forum for asking questions.

7 Responses to “Use TLS/SSL with SMTP in .NET”

  1. leo Says:

    How i can get this class named Limilabs,
    without this it is not usefull for any body

  2. Limilabs support Says:

    @leo
    You can download Mail.dll here:
    http://www.limilabs.com/mail

  3. Hendrik Muus Says:

    something is missing: ERROR: Send hello first
    means do a hello before sending STARTTLS

  4. Limilabs support Says:

    @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.

  5. Tim Black Says:

    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

  6. Limilabs support Says:

    @Tim

    You need to specify correct port to use SSL. From SMTP protocol it’s usually 465. You can also use Smtp.DefaultSSLPort property.

    using (Smtp client = new Smtp())
    {
        client.Connect("smtp.gmail.com", 465, true);
        // which is equivalent to:
        // client.ConnectSSL("smtp.gmail.com");
    
    
    

    To use StartTLS method first connect without SSL (otherwise you’ll get “Unrecognized command” error).

    using (Smtp client = new Smtp())
    {
        client.Connect("smtp.gmail.com", 25, false);
        // which is equivalent to:
        // client.Connect("smtp.example.org");
        client.StartTLS();
    
    

    You can check if server supports StartTLS command with following code:

    bool supportsTLS = client.SupportedExtensions().Contains(SmtpExtension.StartTLS);
    
  7. A connection attempt failed Says:

    […] Use SSL with SMTP […]