Overview of Mail Service Clients in Java
Starting with version 26.10, Aspose.Email for Java includes clients for cloud mail services and mail protocols, along with UnifiedMailClient, a single API that works on top of them. This page lists the clients, explains how they relate to the existing API, and shows how to use them.
Available Clients
Every client is created with a builder and implements java.io.Closeable.
| Service | Client | Package | Connection |
|---|---|---|---|
| IMAP | ImapClient |
com.aspose.email.services.inetmail.client |
Mail protocol library |
| SMTP | SmtpClient |
com.aspose.email.services.inetmail.client |
Mail protocol library |
| POP3 | Pop3Client |
com.aspose.email.services.inetmail.client |
Mail protocol library |
| SendGrid | SendGridClient |
com.aspose.email.services.sendgrid.client |
HTTP |
| Mailgun | MailgunClient |
com.aspose.email.services.mailgun.client |
HTTP |
| Amazon SES | SesClient |
com.aspose.email.services.ses.client |
HTTP |
| Gmail | GmailClient |
com.aspose.email.services.google.mail.client |
HTTP |
| Google Calendar | CalendarClient |
com.aspose.email.services.google.calendar.client |
HTTP |
| Google People | PeopleClient |
com.aspose.email.services.google.people.client |
HTTP |
| Microsoft Graph | MsGraphClient |
com.aspose.email.services.msgraph.client |
HTTP |
| Exchange EWS | EwsClient |
com.aspose.email.services.ews.client |
HTTP |
Clients that connect over HTTP do not depend on a particular HTTP library. To learn how to select one, see HTTP Clients for Mail Services.
Service Clients and the Existing API
The service clients do not replace the classes in the com.aspose.email package, such as ImapClient, SmtpClient, Pop3Client, EWSClient, GmailClient and GraphClient. These classes remain available and are described in their own sections of this guide.
The two sets of classes use different data types:
- A service client works with the data model of its service, for example
GraphMessagefor Microsoft Graph. UnifiedMailClientworks with Aspose.Email types: MailMessage, Appointment, VCardContact and MapiTask.
com.aspose.email, for example ImapClient and GmailClient. Use explicit imports instead of import com.aspose.email.*, or the compiler may pick the wrong class or report an ambiguous reference.
Capabilities of UnifiedMailClient
UnifiedMailClient reaches each service through a provider. Not every service supports every operation:
| Provider | Service | Send | Read | Calendar | Contacts | Tasks |
|---|---|---|---|---|---|---|
MsGraphProvider |
Microsoft Graph | Yes | Yes | Yes | Yes | Yes |
ExchangeEwsProvider |
Exchange EWS | Yes | Yes | Yes | Yes | Yes |
GoogleProvider |
Gmail, Google Calendar, Google People | Yes | Yes | Yes | Yes | No |
InetMailProvider |
SMTP and IMAP | Yes | Yes | No | No | No |
SendGridProvider |
SendGrid | Yes | No | No | No | No |
MailgunProvider |
Mailgun | Yes | No | No | No | No |
AmazonSesProvider |
Amazon SES | Yes | No | No | No | No |
InetMailProvider reads messages over IMAP. POP3 is available only through the standalone Pop3Client.
To check a capability at runtime, call supports with a value of the UnifiedCapability enumeration. Accessing a capability that the provider does not support, for example calling calendar() on SendGridProvider, throws UnsupportedCapabilityException.
Requirements
The clients require Aspose.Email for Java 26.10 or later and Java 8 or later. What else you need depends on the client:
- Clients that connect over HTTP work without additional libraries. To learn how to use another HTTP library, see HTTP Clients for Mail Services.
ImapClient,SmtpClientandPop3Clientrequire Jakarta Mail or JavaMail on the classpath. For details, see Mail Protocol Libraries Setup.
Using a Service Client
The following code sample connects to Microsoft Graph with an application registered in Microsoft Entra ID and lists the subjects of five messages from a mailbox:
import com.aspose.email.services.msgraph.api.QueryOptions;
import com.aspose.email.services.msgraph.auth.ClientCredentialsTokenProvider;
import com.aspose.email.services.msgraph.client.MsGraphClient;
import com.aspose.email.services.msgraph.model.GraphMessage;
import java.util.List;
try (MsGraphClient graph = MsGraphClient.builder()
.tokenProvider(new ClientCredentialsTokenProvider("tenant-id", "client-id", "client-secret"))
.build()) {
List<GraphMessage> messages = graph.users("user@example.com").messages()
.list(QueryOptions.builder().top(5).build());
for (GraphMessage message : messages) {
System.out.println(message.getSubject());
}
}
Using UnifiedMailClient
The following code sample reads the same mailbox through UnifiedMailClient. To work with another service, change the provider:
import com.aspose.email.services.bridge.common.model.MailMessageInfo;
import com.aspose.email.services.bridge.msgraph.provider.MsGraphProvider;
import com.aspose.email.services.unified.UnifiedMailClient;
import com.aspose.ext.http.HttpClientAutoDetect;
import com.aspose.ext.http.IHttpClient;
import java.util.List;
try (IHttpClient http = HttpClientAutoDetect.detect()) {
MsGraphProvider provider = MsGraphProvider.builder()
.httpClient(http)
.tenantId("tenant-id")
.clientId("client-id")
.clientSecret("client-secret")
.userPrincipalName("user@example.com")
.build();
try (UnifiedMailClient client = UnifiedMailClient.builder().provider(provider).build()) {
List<MailMessageInfo> messages = client.mail().inbox().list(5);
for (MailMessageInfo message : messages) {
System.out.println(message.getSubject());
}
}
}
Enabling Logs
Logging is optional. The clients work without any logging library and write nothing to the log.
To see their log output, add SLF4J and a logger implementation to the classpath. The names of the loggers start with com.aspose.email or com.aspose.ext.http.