Rebex Secure Mail

SMTP, IMAP, EWS, POP3, S/MIME .NET library

Download 30-day free trial Buy from $299
More .NET libraries

Back to feature list...

Connecting

The sample code in this section uses the Smtp class. However, same principles apply to Imap, Pop3 and Ews classes.

Connecting to servers using SMTP, IMAP, POP3 or EWS protocol 

To connect to a server running on default port (25 for SMTP, 143 for IMAP, 110 for POP3, 443 for EWS), call the single-parameter Connect method. A plain (unencrypted) connection is established for SMTP, IMAP and POP3. A secured (encrypted) HTTPS connection is established for EWS.

Connecting to SMTP, IMAP or POP3 server
// create SMTP client instance (applies to Rebex.Net.Imap/Pop3 as well)
var client = new Rebex.Net.Smtp();

// connect to a server
client.Connect(hostname);
' create SMTP client instance (applies to Rebex.Net.Imap/Pop3 as well)
Dim client = New Rebex.Net.Smtp()

' connect to a server
client.Connect(hostname)
Connecting to Exchange server using EWS
// create EWS client instance
var client = new Rebex.Net.Ews();

// connect to a server using HTTPS protocol
//  - implicit TLS/SSL mode
client.Connect(hostname);
' create EWS client instance
Dim client = New Rebex.Net.Ews()

' connect to a server using HTTPS protocol
'  - implicit TLS/SSL mode
client.Connect(hostname)
Tip: To connect to a server running on another port, just pass the port number to the Connect method as an additional argument.

Note: Historically, port 25 was the default for SMTP. Recently, another default port of 587 gained traction.

  • Port 25 is used for communication between two SMTP servers. The sender's SMTP server uses it for sending e-mail to recipient's SMTP server. In this case, authentication is not required, but the target SMTP server only accepts emails for it's recipients.
  • Port 587 is used for communication between an e-mail client and the SMTP server used to send its outgoing mail messages. Mail clients use it for submitting e-mail to their SMTP server for delivery. In this case, authentication is usually required, and any valid recipients are accepted. The client's SMTP server then sends the e-mail to the recipients's SMTP server (port 25).
Note: EWS protocol is only supported by Microsoft Exchange and Office 365.

Connecting to servers using explicit or implicit TLS/SSL 

To establish a secure TLS/SSL connection to a server, use the Connect method with SslMode argument.

Note: Rebex.Net.Ews object establishes secure TLS/SSL connection by default. Use Connect method with SslMode.None to establish unsecure (HTTP) connection. Please note that EWS does not support explicit TLS/SSL (use SslMode.Implicit instead).

Tip: Learn about the difference between explicit and implicit TLS/SSL modes.

Establishing secure connection to SMTP, IMAP or POP3 server
// create SMTP client instance (applies to Rebex.Net.Imap/Pop3 as well)
var client = new Rebex.Net.Smtp();

// connect to a server using explicit TLS/SSL
client.Connect(hostname, SslMode.Explicit);

// connect to a server using implicit TLS/SSL
//client.Connect(hostname, SslMode.Implicit);
' create SMTP client instance (applies to Rebex.Net.Imap/Pop3 as well)
Dim client = New Rebex.Net.Smtp()

' connect to a server using explicit TLS/SSL
client.Connect(hostname, SslMode.Explicit)

' connect to a server using implicit TLS/SSL
'client.Connect(hostname, SslMode.Implicit);
Establishing unsecure connection to Exchange server using EWS
// create EWS client instance
var client = new Rebex.Net.Ews();

// connect to a server using HTTP protocol
//  - no TLS/SSL mode
client.Connect(hostname, SslMode.None);
' create EWS client instance
Dim client = New Rebex.Net.Ews()

' connect to a server using HTTP protocol
'  - no TLS/SSL mode
client.Connect(hostname, SslMode.None)

Server certificate is validated automatically by Rebex Secure Mail, but you can customize the validation process as well.

Note: For Rebex.Net.Smtp, if port number is not specified, the 587 is used as a default port number with explicit TLS/SSL mode.

Note: For Rebex.Net.Ews, if port number is not specified, the 80 is used as a default port number for SslMode.None (HTTP protocol).

Tip: For advanced security options, see TLS/SSL core section.

Note: EWS protocol is only supported by Microsoft Exchange and Office 365.

Switching to encrypted communication 

It's also possible to establish a plain (unencrypted) connection first and upgrade to TLS/SSL later using the Secure method. This is not available in EWS.

// create SMTP client instance (applies to Rebex.Net.Imap/Pop3 as well)
var client = new Rebex.Net.Smtp();

// connect to a server on default (not-secured) port 25
client.Connect(hostname);

// secure the connection with TLS/SSL
client.Secure();

// authenticate securely
client.Login(username, password);
' create SMTP client instance (applies to Rebex.Net.Imap/Pop3 as well)
Dim client = New Rebex.Net.Smtp()

' connect to a server on default (not-secured) port 25
client.Connect(hostname)

' secure the connection with TLS/SSL
client.Secure()

' authenticate securely
client.Login(username, password)

Note: Calling (plain/unencrypted) Connect followed by Secure is equivalent to calling Connect(serverName, SslMode.Explicit). This is different when using Rebex.Net.Smtp - calling Connect uses port 25, but calling Connect(serverName, SslMode.Explicit) uses port 587.

Setting connection options 

There are various connection options such as character sets, proxies, SMTP/IMAP/POP3 extensions, and more.

// create SMTP client instance (applies to Rebex.Net.Imap/Pop3/Ews)
// ...

// use SOCKS5 proxy
smtp.Proxy.ProxyType = ProxyType.Socks5;
// ...

// set UTF-8 encoding for commands and responses
smtp.Encoding = Encoding.UTF8;

// allow only TLS 1.1
smtp.Settings.SslAllowedVersions = TlsVersion.TLS11;

// disable Pipelining extension
smtp.EnabledExtensions &= ~SmtpExtensions.Pipelining;

// connect, log in, send emails
// ...
' create SMTP client instance (applies to Rebex.Net.Imap/Pop3/Ews as well)
' ...

' use SOCKS5 proxy
smtp.Proxy.ProxyType = ProxyType.Socks5
' ...

' set UTF-8 encoding for commands and responses
smtp.Encoding = Encoding.UTF8

' allow only TLS 1.1
smtp.Settings.SslAllowedVersions = TlsVersion.TLS11

' disable Pipelining extension
smtp.EnabledExtensions = smtp.EnabledExtensions And Not SmtpExtensions.Pipelining

' connect, log in, send emails
' ...

Getting info about the connection 

Once connected (or authenticated), you can get useful information about the current connection such as server name, server port or user name. Don't be afraid - user's password is not stored with the connection.

// create SMTP client instance (applies to Rebex.Net.Imap/Pop3/Ews as well)
var client = new Rebex.Net.Smtp();

// connect to a server
client.Connect(hostname, SslMode.Explicit);

// display info
Console.WriteLine("Connected to {0}:{1}.", client.ServerName, client.ServerPort);

// authenticate
client.Login(username, password);

// display info to the user
Console.WriteLine("Logged on as {0}.", client.UserName);
' create SMTP client instance (applies to Rebex.Net.Imap/Pop3/Ews as well)
Dim client = New Rebex.Net.Smtp()

' connect to a server
client.Connect(hostname, SslMode.Explicit)

' display info
Console.WriteLine("Connected to {0}:{1}.", client.ServerName, client.ServerPort)

' authenticate
client.Login(username, password)

' display info to the user
Console.WriteLine("Logged on as {0}.", client.UserName)

Getting info about TLS/SSL connection 

To get information about SSL connection, use TlsSocket property.

// create SMTP client instance (applies to Rebex.Net.Imap/Pop3 as well)
var client = new Rebex.Net.Smtp();

// connect to a server
client.Connect(hostname, SslMode.Explicit);

// Cipher property contains a lot of information about the current cipher
Console.WriteLine("TLS cipher info: {0}", client.TlsSocket.Cipher);

// ServerCertificate property provides access to server certificate
// the first certificate in the chain is the server's certificate
CertificateChain chain = client.TlsSocket.ServerCertificate;
Console.WriteLine("Server's certificate info: Subject: {0}, Issuer: {1}",
    chain[0].GetSubject(), chain[0].GetIssuer());
' create SMTP client instance (applies to Rebex.Net.Imap/Pop3 as well)
Dim client = New Rebex.Net.Smtp()

' connect to a server
client.Connect(hostname, SslMode.Explicit)

' Cipher property contains a lot of information about the current cipher
Console.WriteLine("TLS cipher info: {0}", client.TlsSocket.Cipher)

' ServerCertificate property provides access to server certificate
' the first certificate in the chain is the server's certificate
Dim chain As CertificateChain = client.TlsSocket.ServerCertificate
Console.WriteLine("Server's certificate info: Subject: {0}, Issuer: {1}",
                  chain(0).GetSubject(), chain(0).GetIssuer())

Back to feature list...