Java | Guides | Mailtrap Help Center
Java
Learn how to integrate Mailtrap with Java applications using SDK, SMTP, or RESTful API for email sending.
Mailtrap can be integrated with Java apps and projects for email sending with SDK, SMTP, and RESTful API.
The Mailtrap Java SDK is a robust, enterprise-ready library for sending transactional and bulk emails from Java applications. The SDK supports:
Transactional email sending
Add the SDK to your project using your preferred build tool:
Here's a minimal example to send your first email:
Get your API token from your Mailtrap account under Settings โ API Tokens.
To integrate SMTP with your Java app, navigate to the Integrations tab and copy-paste the credentials or ready-made code snippet into your configuration.
SMTP integration is compatible with any Java framework or library that sends emails via SMTP.

For more information, read the SMTP Integration article.
To integrate Mailtrap using RESTful API, use the configuration available among Code samples under the API section.
API integration can be used with any Java framework or library that supports HTTP requests. For more details, refer to the API documentation.

Read more about API integration here.
<dependency>
<groupId>io.mailtrap</groupId>
<artifactId>mailtrap-java</artifactId>
<version>1.1.0</version>
</dependency>implementation 'io.mailtrap:mailtrap-java:1.1.0'implementation("io.mailtrap:mailtrap-java:1.1.0")import io.mailtrap.client.MailtrapClient;
import io.mailtrap.config.MailtrapConfig;
import io.mailtrap.factory.MailtrapClientFactory;
import io.mailtrap.model.request.emails.Address;
import io.mailtrap.model.request.emails.MailtrapMail;
import java.util.List;
public class MailtrapJavaSDKTest {
private static final String TOKEN = "<YOUR MAILTRAP TOKEN>";
private static final String SENDER_EMAIL = "sender@domain.com";
private static final String RECIPIENT_EMAIL = "recipient@domain.com";
public static void main(String[] args) {
final MailtrapConfig config = new MailtrapConfig.Builder()
.token(TOKEN)
.build();
final MailtrapClient client = MailtrapClientFactory.createMailtrapClient(config);
final MailtrapMail mail = MailtrapMail.builder()
.from(new Address(SENDER_EMAIL))
.to(List.of(new Address(RECIPIENT_EMAIL)))
.subject("Hello from Mailtrap Sending!")
.text("Welcome to Mailtrap Sending!")
.build();
// Send an email using Mailtrap Sending API
try {
System.out.println(client.send(mail));
} catch (Exception e) {
System.out.println("Caught exception : " + e);
}
}
}