Skip to content

Documentation

Send email in Java.

Works with: Java 17 or newer. No dependencies beyond the JDK.

Install

Get a key under API keys in the dashboard and verify a sending domain first (how). Then add the library:

pom.xml
<dependency>
<groupId>com.mailtruss</groupId>
<artifactId>mailtruss</artifactId>
<version>0.1.0</version>
</dependency>

Send an email

This sends one message and passes an idempotency key, so repeating the request can never deliver it twice. Requests that are safe to repeat (reads, deletes, and writes with an idempotency key) are retried on 429 and 5xx, honouring Retry-After. The second argument to send is the idempotency key.

Send.java
Mailtruss mt = new Mailtruss(System.getenv("MAILTRUSS_API_KEY"));
Map<String, Object> res = mt.emails.send(Map.of("from", "hello@yourdomain.com", "to", "ana@example.com", "subject", "Welcome", "html", "<p>Hi Ana</p>"), "welcome-ana");

More on what you can put in a message (cc, bcc, attachments, tags, templates, scheduling) is under Sending.

Verify webhook signatures

Mailtruss signs every webhook with the Standard Webhooks scheme. Check the signature before you trust a request. Pass the raw body and a function that reads a header by name.

Webhooks.java
Map<String, Object> event = Webhook.verify(rawBody, request::getHeader, System.getenv("MAILTRUSS_WEBHOOK_SECRET"));

Events, payloads and retries are on the Webhooks page.

Handle errors

Failures are MailtrussException with getName(), getStatusCode() and getRequestId(). Answers are Map<String, Object> exactly as in the API reference. Every error name, with its cause and fix, is in the error catalogue.

Other languages

Last updated

Something wrong or unclear on this page?

Report an issue