Use SSL with POP3

Mail.dll POP3 component 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 POP3 client immediately connects using secure channel,
  • Explicit – where Mail.dll POP3 client connects on unsecured channel first and then secures the communication by issuing STLS 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.2 are acceptable for secure communication. You can change the defaults using Pop3.SSLConfiguration property.

Pop3 client may decide to secure the channel, if POP3 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.

POP3 implicit SSL mode

Mail.dll POP3 component connects using secure SSL channel. You need to know in advance, if the server supports SSL connections – ask your administrator. Typically, POP3 over SSL is associated with port 995, but this is not always the case. You can always specify different, then standard port, using ConnectSSL method overloads.

// C# version

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

class Program
{
    static void Main(string[] args)
    {
        using (Pop3 pop3 = new Pop3())
        {
            pop3.ConnectSSL("server.example.com");
            pop3.Login("user", "password");

            MailBuilder builder = new MailBuilder();
            foreach (string uid in pop3.GetAll())
            {
                var eml = pop3.GetMessageByUID(uid);
                IMail email = builder.CreateFromEml(eml);
                Console.WriteLine("subject: {0}", email.Subject);
            }
            pop3.Close();
        }
    }
};
' VB.NET version

Imports System
Imports Limilabs.Mail
Imports Limilabs.Client.POP3

Public Module Module1
    Public Sub Main(ByVal args As String())

        Using pop3 As New Pop3()
            pop3.ConnectSSL("server.example.com")
            pop3.Login("user", "password")

            Dim builder As New MailBuilder()
            For Each uid As String In pop3.GetAll()
                Dim eml = pop3.GetMessageByUID(uid)
                Dim email As IMail = builder.CreateFromEml(eml)
                Console.WriteLine("subject: {0}", email.Subject)
            Next
            pop3.Close()
        End Using

    End Sub
End Module

POP3 explicit SSL mode

Mail.dll POP3 component connects using clear text channel and secures the channel using SSL by issuing STLS command. Typically standard POP3 port 110 is used, but this is not always the case. You can always specify different then standard port using Connect method overloads.

// C# version

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

class Program
{
    static void Main(string[] args)
    {
        using (Pop3 pop3 = new Pop3())
        {
            pop3.Connect("server.example.com");

            pop3.StartTLS();

            pop3.Login("user", "password");

            MailBuilder builder = new MailBuilder();
            foreach (string uid in pop3.GetAll())
            {
                var eml = pop3.GetMessageByUID(uid);
                IMail email = builder.CreateFromEml(eml);
                Console.WriteLine("subject: {0}", email.Subject);
            }
            pop3.Close();
        }
    }
};
' VB.NET version

Imports System
Imports Limilabs.Mail
Imports Limilabs.Client.POP3

Public Module Module1
    Public Sub Main(ByVal args As String())

        Using pop3 As New Pop3()
            pop3.Connect("server.example.com")

            pop3.STLS()

            pop3.Login("user", "password")

            Dim builder As New MailBuilder()
            For Each uid As String In pop3.GetAll()
                Dim eml = pop3.GetMessageByUID(uid)
                Dim email As IMail = builder.CreateFromEml(eml)
                Console.WriteLine("subject: {0}", email.Subject)
            Next
            pop3.Close()
        End Using

    End Sub
End Module

After you connect, you can check, if your POP3 server supports explicit SSL using following code:

// C# version

bool supportsSTLS = pop3.SupportedExtensions()
   .Contains(Pop3Extension.STLS);

' VB.NET version

Dim supportsSTLS As Boolean = pop3.SupportedExtensions() _
   .Contains(Pop3Extension.STLS)

You can read more here on how to know which extensions does your server support.

Common problems

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

Tags: , , , , ,

2 Responses to “Use SSL with POP3”

  1. A connection attempt failed Says:

    […] Use SSL with POP3 […]

  2. Receive emails using POP3 | Blog | Limilabs Says:

    […] First thing you need to do is to connect to your POP3 server. Use Connect(string host) method to connect to the server. Typically POP3 server is working on port 110. You can use Connect(string host, int port) overload when you need to specify different port, or ConnectSSL methods to use POP3 over SSL. […]

Questions?

Consider using our Q&A forum for asking any questions.