Use SSL with POP3

Ultimate Mail POP3 Library supports Secure Socket Layer (SSL) and Transport Layer Security (TLS) protocols to establish secure connections between the clients and POP3 servers. People usually say SSL. But in fact, they mean TLS. SSL v3 is the newest version introduced in 1996, while TLS was first introduced in 1999 and its latest version is v1.2 released in 2008.

There are two modes in which ComponentPro.Mail.dll can work:

  • Implicit – where a POP3 client use a secure channel to immediately establish a connection to the server. The client connects, the SSL handshake occurs, and if successful the secure communication continues for the session. With implicit SSL, the POP3 server specifies a dedicated port for the secure connections from clients. By doing so, the connections require less overhead.
  • Explicit – where it establishes a normal connection to the server, usually on the standard POP3 110 port. Then it issues STARTTLS command to switch to SSL mode. The server will response to that command, the negotiation is clear and then the establish an SSL session. The connection is only securely upgraded when the server supports the mechanism and response properly. From here the communication between the client and server is secured until it's downgraded.

In both cases, by default, SSL 3.0 and TLS 1.0, 1.1 & 1.2 are acceptable for secure communication. You can change the defaults using Config property of the Pop3 class.

POP3 Implicit SSL mode

In order to connect to a mail server using Implicit SSL mode, it's advised to know in advance whether the server supports Implicit SSL. Typically, port 995 is dedicated for POP3 Implicit SSL connections. However if it's not the case, the Connect method overloads also accept custom ports other than 995.

using System;
using System.Text;
using ComponentPro.Net;
using ComponentPro.Net.Mail;

...

// Create a new instance of the Pop3 class.
Pop3 client = new Pop3();

// Connect to the server.
client.Connect("myserver", 993, SslSecurityMode.Implicit);

// Login to the server.
client.Authenticate("user", "password");

StringBuilder sb = new StringBuilder();

Pop3MessageCollection list = client.ListMessages(Pop3EnvelopeParts.Size | Pop3EnvelopeParts.UniqueId);
for (int i = 0; i < list.Count; i++)
{
    sb.AppendFormat("{0} - {1}\r\n", i + 1, list[i].UniqueId);
}

Console.WriteLine(sb.ToString());

// Close the connection.
client.Disconnect();
Imports System.Text
Imports ComponentPro.Net
Imports ComponentPro.Net.Mail

...

' Create a new instance of the Pop3 class.
Dim client As New Pop3()

' Connect to the server.
client.Connect("myserver", 993, SslSecurityMode.Implicit)

' Login to the server.
client.Authenticate("user", "password")

Dim sb As New StringBuilder()

Dim list As Pop3MessageCollection = client.ListMessages(Pop3EnvelopeParts.Size Or Pop3EnvelopeParts.UniqueId)
For i As Integer = 0 To list.Count - 1
    sb.AppendFormat("{0} - {1}" & vbCrLf, i + 1, list(i).UniqueId)
Next i

Console.WriteLine(sb.ToString())

' Close the connection.
client.Disconnect()

POP3 Explicit SSL mode

The library connects using clear text channel and issues STARTTLS command to secure the channel. The standard POP3 port 110 or 587 is usually used for Explicit SSL mode. But if you need to specify a different port, you can pass the custom port number to the Connect method overloads.

using System;
using System.Text;
using ComponentPro.Net;
using ComponentPro.Net.Mail;

...

// Create a new instance of the Pop3 class.
Pop3 client = new Pop3();

// Connect to the server.
client.Connect("myserver", 143, SslSecurityMode.Explicit);

// Login to the server.
client.Authenticate("user", "password");

StringBuilder sb = new StringBuilder();

Pop3MessageCollection list = client.ListMessages(Pop3EnvelopeParts.Size | Pop3EnvelopeParts.UniqueId);
for (int i = 0; i < list.Count; i++)
{
    sb.AppendFormat("{0} - {1}\r\n", i + 1, list[i].UniqueId);
}

Console.WriteLine(sb.ToString());

// Close the connection.
client.Disconnect();
Imports System.Text
Imports ComponentPro.Net
Imports ComponentPro.Net.Mail

...

' Create a new instance of the Pop3 class.
Dim client As New Pop3()

' Connect to the server.
client.Connect("myserver", 143, SslSecurityMode.Explicit)

' Login to the server.
client.Authenticate("user", "password")

Dim sb As New StringBuilder()

Dim list As Pop3MessageCollection = client.ListMessages(Pop3EnvelopeParts.Size Or Pop3EnvelopeParts.UniqueId)
For i As Integer = 0 To list.Count - 1
    sb.AppendFormat("{0} - {1}" & vbCrLf, i + 1, list(i).UniqueId)
Next i

Console.WriteLine(sb.ToString())

' Close the connection.
client.Disconnect()

After you connect, you can check, if your server supports explicit SSL using the SupportedFeatures property as shown in following code:

var isExplicitSslSupported = (client.SupportedFeatures & ImapFeatures.ExplicitSecurity) != 0;

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.

45-Day Money Back Guarantee

We will refund your full money in 45 days
if you are not satisfied with our products

Buy Now

Dont miss out Get update on new articles and other opportunities