Connect to POP3 Server
The Pop3Client class allows applications to manage email boxes using the Post Office Protocol, Version 3 (POP3). To connect to a server, use the Pop3Client class. The Pop3Client class is the major entry for developers who want to add POP3 management to their .NET applications. This article explains how to use it. To connect to a POP3 server:
- Create an instance of the Pop3Client class.
- 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-Java
// Create an instance of the Pop3Client class
Pop3Client client = new Pop3Client();
// Specify host, username, password, Port and SecurityOptions for your client
client.setHost("pop.gmail.com");
client.setUsername("your.username@gmail.com");
client.setPassword("your.password");
client.setPort(995);
client.setSecurityOptions(SecurityOptions.Auto);
System.out.println("Connected to POP3 server.");
Connecting to SSL Server
Connecting to a POP3 Server described how to connect to a POP3 server in three simple steps:
- Create an instance of the Pop3Client class.
- Specify the host, username, and password.
The process for connecting to an SSL enabled POP3 server is similar but requires that you set another few properties:
- SecurityOptions
- Port
To connect to an SSL enabled POP3 server, use the Pop3Client class and 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-Java
// Create an instance of the Pop3Client class
Pop3Client client = new Pop3Client();
// Specify host, username and password, Port and SecurityOptions for your client
client.setHost("pop.gmail.com");
client.setUsername("your.username@gmail.com");
client.setPassword("your.password");
client.setPort(995);
client.setSecurityOptions(SecurityOptions.Auto);
System.out.println("Connecting to POP3 server using SSL.");
Connecting with an 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’s password when checking email. APOP authentication does not require the account password to be sent as plain text to the POP3 mail server.
Connecting to Server via Proxy
Proxy servers are very common for communicating with the outside world. In such cases, 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. This article provides a working sample of retrieving email using a proxy mail server. To retrieve email via a proxy server:
- Initialize Proxy with the required information, that is, proxy address, port, and SOCKS version.
- Initialize Pop3Client with the host address, user name, password, and any other settings.
- Set the client’s Proxy property 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-Java
// Create an instance of the Pop3Client class
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, SocksVersion.SocksV5);
client.setProxy(proxy);
Pop3MailboxInfo mailboxInfo = client.getMailboxInfo();
Connecting to Server via HTTP Proxy
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
HttpProxy proxy = new HttpProxy("18.222.124.59", 8080);
try (Pop3Client client = new Pop3Client("imap.domain.com", "username", "password")) {
client.setProxy(proxy);
Pop3MailboxInfo mailboxInfo = client.getMailboxInfo();
}
How to 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.
try (Pop3Client pop3Client = new Pop3Client("host", 995, "username", "password", SecurityOptions.Auto))
{
pop3Client.setTimeout(60000); // 60 seconds
// some code...
}