Setting Up Mail Protocol Libraries for IMAP, SMTP and POP3 Clients in Java
ImapClient, SmtpClient and Pop3Client from the com.aspose.email.services.inetmail.client package work through Jakarta Mail or JavaMail. Aspose.Email does not include these libraries, so add one of them to your project. Without a library, the build method of the client builder throws IllegalStateException with a message that names the libraries to add.
Choosing a Library
| Library | Package | Java |
|---|---|---|
| JavaMail 1.6 | javax.mail |
8 or later |
| Jakarta Mail 2.0 | jakarta.mail |
8 or later |
| Jakarta Mail 2.1 with Eclipse Angus 2 | jakarta.mail |
11 or later |
The following dependencies add each of these libraries:
<!-- JavaMail 1.6 -->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
<!-- Jakarta Mail 2.0 -->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>jakarta.mail</artifactId>
<version>2.0.1</version>
</dependency>
<!-- Jakarta Mail 2.1 with Eclipse Angus 2 -->
<dependency>
<groupId>org.eclipse.angus</groupId>
<artifactId>angus-mail</artifactId>
<version>2.0.3</version>
</dependency>
Maven adds the libraries that these artifacts depend on. If you add JAR files to the classpath manually, also add their dependencies. For example, JavaMail 1.6 on Java 11 or later also requires the JavaBeans Activation Framework (javax.activation).
jakarta.mail-api is on the classpath, build throws IllegalStateException: No provider of jakarta.mail.util.StreamProvider was found. Do not use Eclipse Angus 1 on Java 8, because sending messages does not work with this combination. On Java 8, use JavaMail 1.6 or Jakarta Mail 2.0.
Automatic Selection
You do not specify a library in the code. When you call build, the client checks the classes available at runtime and selects the library in the following order:
- A library with the
jakarta.mailpackage. - A library with the
javax.mailpackage.
Selection depends on the package and not on the name of the artifact. For example, com.sun.mail:jakarta.mail:1.6.7 contains the javax.mail package, so the client uses it as JavaMail.
Connection Settings
The builders of all three clients have the same connection settings. ImapClient and Pop3Client require host and credentials. SmtpClient requires only host: without credentials, it connects to the server without authentication.
| Setting | Builder method | ImapClient default |
SmtpClient default |
Pop3Client default |
|---|---|---|---|---|
| Server name | host |
None | None | None |
| Port | port |
993 | 587 | 995 |
| Security | security, ssl, starttls |
SSL | STARTTLS | SSL |
| Credentials | credentials |
None | None | None |
| Connection timeout, in milliseconds | connectTimeout |
30 000 | 30 000 | 30 000 |
| Read timeout, in milliseconds | readTimeout |
60 000 | 60 000 | 60 000 |
Connection security is set by the SecurityType enumeration:
| Value | Connection |
|---|---|
SSL |
Encrypted from the start |
STARTTLS |
Starts unencrypted and then switches to encryption. If the server does not support this, the operation fails. |
NONE |
Not encrypted |
The following code sample sets all connection settings for ImapClient:
import com.aspose.email.services.inetmail.SecurityType;
import com.aspose.email.services.inetmail.auth.PasswordCredentials;
import com.aspose.email.services.inetmail.client.ImapClient;
try (ImapClient imap = ImapClient.builder()
.host("imap.example.com")
.port(993)
.security(SecurityType.SSL)
.credentials(new PasswordCredentials("user@example.com", "password"))
.connectTimeout(10000)
.readTimeout(30000)
.build()) {
System.out.println(imap.listFolders().size());
}
A client connects when it performs its first operation. For this reason, build succeeds even if the server is unavailable or the credentials are wrong, and the error appears at the first operation as MailClientException.
Authenticating with a Password
To sign in with a user name and a password, use PasswordCredentials:
import com.aspose.email.services.inetmail.auth.PasswordCredentials;
import com.aspose.email.services.inetmail.client.ImapClient;
try (ImapClient imap = ImapClient.builder()
.host("imap.example.com")
.credentials(new PasswordCredentials("user@example.com", "password"))
.build()) {
System.out.println(imap.listFolders().size());
}
Authenticating with OAuth 2.0
To sign in with an OAuth 2.0 access token through the XOAUTH2 mechanism, use OAuth2Credentials. Get the token from your identity provider before you create the client. The client does not refresh the token, so create a new client when the token expires.
import com.aspose.email.services.inetmail.auth.OAuth2Credentials;
import com.aspose.email.services.inetmail.client.ImapClient;
try (ImapClient imap = ImapClient.builder()
.host("imap.example.com")
.credentials(new OAuth2Credentials("user@example.com", "access-token"))
.build()) {
System.out.println(imap.listFolders().size());
}
Next Steps
To learn how to use the clients, see the following pages: