Connect to IMAP Server C#

Internet Message Access Protocol (IMAP) is the most commonly used protocol for retrieving messages from an email server. In order to access the mailbox from your client applications, you would first need to establish a connection with the IMAP server. To achieve this, this article covers how to connect to an IMAP server via SOCKS or HTTP proxy using C#. Furthermore, you will learn how to connect to an SSL-enabled IMAP server programmatically.

C# API to Connect IMAP Server

Aspose.Email for .NET is a feature-rich API that lets you implement email clients from within your .NET applications. In addition, it lets you connect to IMAP servers and access the mailboxes via SOCKS or HTTP proxies. You can either download the API or install it via NuGet.

PM> Install-Package Aspose.Email

Connect to IMAP Server in C#

The following are the steps to connect to an IMAP server using Aspose.Email for .NET.

  1. Create an instance of the ImapClient class.
  2. Specify the hostname, username, and password in the ImapClient constructor.
  3. Use the ImapClient object to access the mailbox.

The following code sample shows how to connect to an IMAP server using C#.

// Connect to IMAP server using host, user and password
ImapClient client = new ImapClient("localhost", "user", "password");
// Select folder
client.SelectFolder("Inbox");
view raw connect-imap.cs hosted with ❤ by GitHub

Connect to IMAP Server via SOCKS Proxy

In various cases, email servers are not directly accessible and you have to use a proxy server. In order to connect the IMAP server via SOCKS proxy, Aspose.Email for .NET supports SOCKS version 4, 4a, and 5. The following are the steps to connect to an IMAP server via SOCKS proxy.

  1. Create an instance of the ImapClient class.
  2. Specify the hostname, username, and password in the ImapClient constructor.
  3. Create an instance of SocksProxy and initialize it with proxy address, port, and SOCKS version.
  4. Set proxy for IMAP using ImapClient.Proxy property.

The following code sample shows how to connect to an IMAP server via SOCKS proxy.

// Connect and log in to IMAP and set SecurityOptions
ImapClient client = new ImapClient("imap.domain.com", "username", "password");
client.SecurityOptions = SecurityOptions.Auto;
// Configure SOCKS proxy
string proxyAddress = "192.168.203.142"; // proxy address
int proxyPort = 1080; // proxy port
SocksProxy proxy = new SocksProxy(proxyAddress, proxyPort, SocksVersion.SocksV5);
// Set the proxy for IMAP
client.Proxy = proxy;
// Select folder
client.SelectFolder("Inbox");

Connect to IMAP Server via HTTP Proxy

The following are the steps to connect to an IMAP server via HTTP proxy.

  1. Create an instance of the ImapClient class.
  2. Specify the hostname, username, and password in the ImapClient constructor.
  3. Create an instance of HttpProxy and initialize it with the proxy address and port number.
  4. Set HTTP proxy for IMAP using ImapClient.Proxy property.

The following code sample shows how to connect to an IMAP server via HTTP proxy.

// Configure HTTP proxy
HttpProxy proxy = new HttpProxy("18.222.124.59", 8080);
// Create and configure ImapClient and set HTTP proxy
using (ImapClient client = new ImapClient("imap.domain.com", "username", "password"))
{
// Set proxy
client.Proxy = proxy;
// Select folder
client.SelectFolder("Inbox");
}

Connect to SSL Enabled IMAP Server in C#

The following are the steps to connect to an SSL-enabled IMAP server using C#.

  1. Create an instance of the ImapClient class.
  2. Specify the hostname, username, and password in the ImapClient constructor.
  3. Set ImapClient.SecurityOptions property to SecurityOptions.SSLImplicit.

The following code sample shows how to connect to an SSL-enabled IMAP server.

// Create an instance of the ImapClient class
ImapClient client = new ImapClient("imap.domain.com", 993, "user@domain.com", "pwd");
// Set the security mode to implicit
client.SecurityOptions = SecurityOptions.SSLImplicit;
// Select folder
client.SelectFolder("Inbox");

Get a Free API License

You can try and use Aspose.Email for .NET for free by getting a temporary license.

Conclusion

In this article, you have learned how to connect to an IMAP server using C#. Furthermore, the step-by-step guide and code samples have demonstrated how to connect to an IMAP server via SOCKS or HTTP proxy. You can explore more about the API using documentation. For any questions or queries, feel free to let us know via our forum.

See Also