Rebex IMAP

IMAP, SMTP, MIME, S/MIME libraries for .NET

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

Back to feature list...

Connecting

Connecting to servers using IMAP protocol 

To connect to an IMAP server running on default port (143), call the single-parameter Connect method. A plain (unencrypted) connection is established.

// create IMAP client instance
var client = new Rebex.Net.Imap();

// connect to a server
client.Connect(hostname);
' create IMAP client instance
Dim client = New Rebex.Net.Imap()

' connect to a server
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.

Connecting to servers using explicit or implicit TLS/SSL 

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

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

// create IMAP client instance
var client = new Rebex.Net.Imap();

// 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 IMAP client instance
Dim client = New Rebex.Net.Imap()

' 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);

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

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

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.

// create IMAP client instance
var client = new Rebex.Net.Imap();

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

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

// authenticate securely
client.Login(username, password);
' create IMAP client instance
Dim client = New Rebex.Net.Imap()

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

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

' authenticate securely
client.Login(username, password)

Setting connection options 

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

// create IMAP client instance
// ...

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

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

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

// connect, log in, retrieve emails
// ...
' create IMAP client instance
' ...

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

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

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

' 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 IMAP client instance
var client = new Rebex.Net.Imap();

// 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 IMAP client instance
Dim client = New Rebex.Net.Imap()

' 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 IMAP client instance
var client = new Rebex.Net.Imap();

// 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 IMAP client instance
Dim client = New Rebex.Net.Imap()

' 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...