Default ports for email protocols

Here’s the list of three most common email protocols and their default ports:

Protocol Plain text SSL
IMAP * 143 993
POP3 * 110 995
SMTP 587 or 25 ** 465

* Check the differences between POP3 and IMAP

** SMTP (and port 25) was originally designed for transfer, not submission. So yet another port (587) was defined for message submission.

Establishing connection using default port is easy:

// C#

client.Connect("mail.example.com");
' VB.NET

client.Connect("mail.example.com")

If you need to specify different port, just use overloaded version of the Connect method:

// C#

client.Connect("mail.example.com", 999);
' VB.NET

client.Connect("mail.example.com", 999)

Implicit SSL mode

Establishing connection encrypted with SSL using default port is easy:

// C#
client.ConnectSSL("mail.example.com");
' VB.NET
client.ConnectSSL("mail.example.com")

If you need to specify different port, just use overloaded version of ConnectSSL method:

// C#
client.ConnectSSL("mail.example.com", 999);
' VB.NET
client.ConnectSSL("mail.example.com", 999)

Explicit SSL mode (aka TLS)

Sometimes your server might require that you first connect on non SSL (plain text) port using regular Connect method, and then start SSL/TLS negotiation using STARTTLS (or STLS in case of POP3) command. This is called explicit SSL mode and sometimes is referred to as TLS:

// C#
client.Connect("mail.example.com");
client.StartTLS();
' VB.NET
client.Connect("mail.example.com")
client.StartTLS()

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

Self-signed certificates

Remember that you can ignore SSL certificate errors (for example when your server uses self-signed certificates) using ServerCertificateValidate event:

// C#
client.ServerCertificateValidate +=
    (sender, e) => { e.IsValid = true; };
' VB.NET

AddHandler client.ServerCertificateValidate, AddressOf Validate

Private Sub Validate(ByVal sender As Object, ByVal e As ServerCertificateValidateEventArgs)
    e.IsValid = True
End Sub

Tags:       

Questions?

Consider using our Q&A forum for asking questions.