Use SSL with SMTP

Mail.dll SMTP client supports Secure Socket Layer (SSL) and Transport Layer Security (TLS) protocols to authenticate the server and secure client-server communication.

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.

In both cases, by default, either Secure Sockets Layer (SSL) 3.0 or Transport Layer Security (TLS) 1.0 are acceptable for secure communication. You can change the defaults by 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.

Implicit mode:

// C# version

using Limilabs.Mail;
using Limilabs.Client.POP3;

class Program
{
    static void Main(string[] args)
    {
        using (Smtp smtp = new Smtp())
        {
            smtp.ConnectSSL("server.example.com");

            smtp.Login("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("server.example.com")

            smtp.Login("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

Explicit mode:

// 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("server.example.com");

            smtp.StartTLS();

            smtp.Login("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("server.example.com")

            smtp.StartTLS()

            smtp.Login("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

If you are using self-signed certificates you may encounter this error: The remote certificate is invalid according to the validation procedure.

Tags: , , , ,

7 Responses to “Use SSL with SMTP”

  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 [...]

Leave a Reply