Use TLS/SSL with POP3 in .NET
Mail.dll POP3 .NET email component supports Secure Socket Layer (SSL) and Transport Layer Security (TLS) protocols to authenticate the server and secure client-server email downloads.
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.1, 1.2, 1.3 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.
POP3 implicit TLS/SSL mode
Mail.dll POP3 component connects using secure TLS/SSL channel. You need to know in advance, if the server supports TLS/SSL connections – ask your administrator. Typically, POP3 over TLS/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("pop.example.com");
pop3.UseBestLogin("user", "password");
MailBuilder builder = new MailBuilder();
foreach (string uid in pop3.GetAll())
{
var eml = pop3.GetMessageByUID(uid);
IMail email = builder.CreateFromEml(eml);
string subject = 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("pop.example.com")
pop3.UseBestLogin("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)
Dim subject As String = email.Subject
Next
pop3.Close()
End Using
End Sub
End Module
POP3 explicit TLS/SSL mode
Mail.dll POP3 component connects using clear text channel and secures the channel using TLS/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("pop.example.com");
pop3.StartTLS();
pop3.UseBestLogin("user", "password");
MailBuilder builder = new MailBuilder();
foreach (string uid in pop3.GetAll())
{
var eml = pop3.GetMessageByUID(uid);
IMail email = builder.CreateFromEml(eml);
string subject = 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("pop.example.com")
pop3.STLS()
pop3.UseBestLogin("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)
Dim subject As String = email.Subject
Next
pop3.Close()
End Using
End Sub
End Module
After you connect, you can check, if your POP3 server supports explicit TLS/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.
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
February 1st, 2015 at 18:44
[…] Use SSL with POP3 […]
May 12th, 2016 at 11:43
[…] 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. […]