Connect to POP3 Server

Connect to POP3 Server

The Pop3Client class allows applications to manage email boxes using the Post Office Protocol, Version 3 (POP3). This class is the major entry for developers who want to add POP3 management to their .NET applications.

To connect to a POP3 server:

  1. Create an instance of the Pop3Client class.
  2. Specify the host, username, and password in the Pop3Client instance.

The following code snippet shows you how to connect with the POP3 server.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Create an instance of the Pop3Client class
Pop3Client client = new Pop3Client();
// Specify host, username, password, Port and SecurityOptions for your client
client.Host = "pop.gmail.com";
client.Username = "your.username@gmail.com";
client.Password = "your.password";
client.Port = 995;
client.SecurityOptions = SecurityOptions.Auto;
Console.WriteLine(Environment.NewLine + "Connected to POP3 server.");

Connect to SSL server

The process for connecting to an SSL enabled POP3 server is similar but requires that you set another few properties:

To connect to an SSL enabled POP3 server, set the SecurityOptions and Port properties. The following code snippet shows you how to connect to an SSL enables POP3 server.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Create an instance of the Pop3Client class
Pop3Client client = new Pop3Client();
// Specify host, username and password, Port and SecurityOptions for your client
client.Host = "pop.gmail.com";
client.Username = "your.username@gmail.com";
client.Password = "your.password";
client.Port = 995;
client.SecurityOptions = SecurityOptions.Auto;
Console.WriteLine(Environment.NewLine + "Connecting to POP3 server using SSL.");

Connect to APOP Server

POP stands for Post Office Protocol. APOP stands for Authenticated Post Office Protocol. APOP is an extended version of the POP3 server setting that encrypts your username and password and uses an authentication mechanism designed to protect your POP3 account password when checking email. APOP authentication does not require the account password to be sent as plain text to the POP3 mail server.

Connect to Server via Proxy

Proxy addresses are used for email clients to access mailboxes over the Internet. Aspose.Email provides support for versions 4, 4a and 5 of the SOCKS proxy protocol.

To retrieve email via a proxy server:

  1. Initialize Proxy with the required information, that is, proxy address, port, and SOCKS version.
  2. Initialize Pop3Client with the host address, user name, password, and any other settings.
  3. Set the Proxy property of a client to the Proxy object created above.

The following code snippet shows you how to retrieve email via proxy server.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
Pop3Client client = new Pop3Client("pop.domain.com", "username", "password");
// Set proxy address, Port and Proxy
string proxyAddress = "192.168.203.142";
int proxyPort = 1080;
SocksProxy proxy = new SocksProxy(proxyAddress, proxyPort, Aspose.Email.Protocols.Proxy.SocksVersion.SocksV5);
client.SocksProxy = proxy;
Pop3MailboxInfo mailboxInfo = client.GetMailboxInfo();

Connect to Server via HTTP Proxy

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
HttpProxy proxy = new HttpProxy("18.222.124.59", 8080);
using (Pop3Client client = new Pop3Client("imap.domain.com", "username", "password"))
{
client.Proxy = proxy;
Pop3MailboxInfo mailboxInfo = client.GetMailboxInfo();
}

Connect with CRAM-MD5 Authentication

Using CRAM-MD5 authentication, Aspose.Email for .NET allows users to securely authenticate and access email servers supporting this authentication method. The code sample below shows how to use the mechanism in your project:

popClient.AllowedAuthentication = Pop3KnownAuthenticationType.CramMD5;

List Server Extensions

Pop3Client lets you retrieve the server extensions that a server supports such as IDLE, UNSELECT, QUOTA, etc. This helps in identifying the availability of an extension before using the client for that particular functionality. The GetCapabilities() method returns the supported extension types in the form of a string array.

Retrieve Server Extensions

The following code sample demonstrates retrieving server extensions using POP3Client for Gmail server.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Connect and log in to POP3
Pop3Client client = new Pop3Client("pop.gmail.com", "username", "password");
client.SecurityOptions = SecurityOptions.Auto;
client.Port = 993;
string[] getCaps = client.GetCapabilities();
foreach (string item in getCaps)
{
Console.WriteLine(item);
}

Set Timeout for Mail Operations

Each mail operation takes some time depending on many factors (network delays, data size, server performance, etc.). You can set a timeout for all mail operations. The code example below shows you how to do that using the Timeout property. Note: you should not set large values to avoid long waits in your application.

using (Pop3Client pop3Client = new Pop3Client("host", 995, "username", "password", SecurityOptions.Auto))
{
    pop3Client.Timeout = 60000; // 60 seconds

    // some code...
}

Use Cryptographic Protocols with POP3 Client

Aspose.Email supports SSL (obsolete) and TLS cryptographic protocols to provide communications security. You can enable cryptographic encryption to protect data exchange between your application and mail servers.

NOTE: You should set only those versions of the protocol, which are supported by .NET Framework. If some versions of the cryptographic protocol are not supported by your current version of .NET Framework, they will be ignored and skipped. In this case, exceptions won’t be generated. Please use SetSupportedEncryptionUnsafe method if you want to set the protocols without any compatibility checks.

The code example below shows you how to set TLS 1.3 for Pop3Client class instance.

using (Pop3Client pop3Client = new Pop3Client("host", 995, "username", "password", SecurityOptions.Auto))
{
    pop3Client.SupportedEncryption = EncryptionProtocols.Tls13;

    // some code...
}

In case of a specified encryption protocol is not supported in the current version of .NET Framework, the difference in behavior between SetSupportedEncryptionUnsafe method and SupportedEncryption property is the following: