This SDK offers integration with the official API for Mailtrap.
Quickly add email sending functionality to your Java application with Mailtrap.
Requires JDK 11 or higher.
As Maven dependency:
<dependency>
<groupId>io.mailtrap</groupId>
<artifactId>mailtrap-java</artifactId>
<version>1.0.0</version>
</dependency>
As Gradle Groovy dependency:
implementation 'io.mailtrap:mailtrap-java:1.0.0'
As Gradle Kotlin DSL dependency:
implementation("io.mailtrap:mailtrap-java:1.0.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";
private static final String REPLY_TO_EMAIL = "reply_to@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)))
.replyTo(new Address(REPLY_TO_EMAIL, "Vincent Vega"))
.subject("Hello from Mailtrap Sending!")
.text("Welcome to Mailtrap Sending!")
.build();
// Send email using Mailtrap Sending API
try {
System.out.println(client.send(mail));
} catch (Exception e) {
System.out.println("Caught exception : " + e);
}
// Or send email using Mailtrap Testing API
try {
long inboxId = 1000001L;
// Either instantiate a new client
MailtrapClient sandboxClient = MailtrapClientFactory.createMailtrapClient(
new MailtrapConfig.Builder()
.sandbox(true)
.inboxId(inboxId)
.token(TOKEN)
.build());
System.out.println(sandboxClient.send(mail));
// Or reuse already created client
client.switchToEmailTestingApi(inboxId);
System.out.println(client.send(mail));
// Or use Testing API directly
System.out.println(client.testingApi().emails().send(mail, inboxId));
} catch (Exception e) {
System.out.println("Caught exception : " + e);
}
}
}
Refer to the examples
folder for the source code of this and other advanced examples.
You can find the API reference here.
Bug reports and pull requests are welcome on GitHub. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
The package is available as open source under the terms of the MIT License.
Everyone interacting in the Mailtrap project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.