Reading Messages Using the POP3 Service Client in Java
Pop3Client from the com.aspose.email.services.inetmail.client package reads messages from the inbox of a POP3 server. It requires a mail protocol library on the classpath. For details, see Mail Protocol Libraries Setup.
UnifiedMailClient does not use POP3, so Pop3Client is available only as a standalone client.
com.aspose.email package has a class with the same name, which is described in Working with POP3 Client. Use explicit imports to select the class you need.
Connecting to a Server
The following code sample creates a client and prints the number of messages in the inbox. Other code samples on this page use the pop3 variable created here.
import com.aspose.email.services.inetmail.auth.PasswordCredentials;
import com.aspose.email.services.inetmail.client.Pop3Client;
try (Pop3Client pop3 = Pop3Client.builder()
.host("pop.example.com")
.port(995)
.ssl()
.credentials(new PasswordCredentials("user@example.com", "password"))
.build()) {
System.out.println(pop3.getMessageCount());
}
The client connects when it performs its first operation, not when you call build. It keeps the connection until you close the client.
Listing Messages
listMessages returns a list of InetMessageInfo objects, which contain the main properties of each message without its body. Use the value returned by getMsgnum to access the message:
import com.aspose.email.services.inetmail.model.InetMessageInfo;
for (InetMessageInfo info : pop3.listMessages()) {
System.out.println(info.getMsgnum() + ": " + info.getSubject() + ", " + info.getSize() + " bytes");
}
Message numbers are valid only for the current connection. After you reconnect, the same message can have a different number.
Reading a Message
To get a complete message with its body and attachments, call getMessage with the message number:
import com.aspose.email.services.inetmail.model.InetMessage;
InetMessage message = pop3.getMessage(1);
System.out.println(message.getSubject());
System.out.println(message.getTextBody());
Message numbers start at 1. For a number that does not match a message in the inbox, getMessage and deleteMessage throw MailClientException.
Deleting a Message
To mark a message for deletion, call deleteMessage with the message number:
pop3.deleteMessage(1);
getMessageCount still includes them.
Handling Errors
When an operation fails, for example because of a wrong password, the client throws MailClientException, which extends java.io.IOException:
import com.aspose.email.services.inetmail.MailClientException;
import com.aspose.email.services.inetmail.auth.PasswordCredentials;
import com.aspose.email.services.inetmail.client.Pop3Client;
import java.io.IOException;
try (Pop3Client pop3 = Pop3Client.builder()
.host("pop.example.com")
.port(995)
.ssl()
.credentials(new PasswordCredentials("user@example.com", "wrong-password"))
.build()) {
System.out.println(pop3.getMessageCount());
} catch (MailClientException e) {
System.out.println(e.getMessage() + ": " + e.getCause().getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}