millionsend-java
Official Java SDK for MillionSend — a self-hostable, Resend-compatible email API on AWS SES.
The API is wire-compatible with Resend, and this SDK deliberately mirrors the
shape of resend-java, so migrating is
mostly a find-and-replace: swap the dependency and the class name. MillionSend
Cloud works with just the key; a self-hosted instance also sets its origin.
Install
Maven:
<dependency>
<groupId>com.millionsend</groupId>
<artifactId>millionsend-java</artifactId>
<version>0.10.2</version>
</dependency>
Gradle:
implementation 'com.millionsend:millionsend-java:0.10.2'
Requires Java 11+.
Quickstart
import com.millionsend.MillionSend;
import com.millionsend.MillionSendException;
import com.millionsend.model.CreateEmailOptions;
import com.millionsend.model.CreateEmailResponse;
MillionSend ms = new MillionSend("ms_123"); // MillionSend Cloud
// MillionSend ms = new MillionSend("ms_123", "https://mail.acme.dev"); // self-hosted
try {
CreateEmailResponse sent = ms.emails().send(
CreateEmailOptions.builder()
.from("Acme <onboarding@acme.dev>")
.to("success@simulator.amazonses.com")
.subject("Hello from MillionSend")
.html("<strong>It works!</strong>")
.build());
System.out.println("sent " + sent.getId());
} catch (MillionSendException e) {
System.err.println(e.getName() + ": " + e.getMessage());
}
Configuration
new MillionSend(); // apiKey + baseUrl from the environment
new MillionSend(apiKey); // baseUrl from MILLIONSEND_BASE_URL or MillionSend Cloud
new MillionSend(apiKey, baseUrl); // explicit base URL (self-hosted)
new MillionSend(apiKey, baseUrl, true); // also accept a non-loopback http:// base URL
apiKey falls back to MILLIONSEND_API_KEY. A missing key throws IllegalArgumentException at construction.
baseUrl falls back to MILLIONSEND_BASE_URL, then MillionSend Cloud (https://api.millionsend.com). Self-hosting? Set it to your instance's origin.
- Plain
http:// is only accepted for loopback hosts (localhost, 127.0.0.1, ::1); any other http:// URL throws IllegalArgumentException at construction, since the API key is sent as a bearer header. Pass allowInsecureHttp = true (third constructor argument) to talk to a non-TLS instance elsewhere (e.g. inside a private network).
Error handling
Every call throws a single checked MillionSendException on a non-2xx response.
It carries the API's error shape:
getName() — a stable snake_case code you can switch on (validation_error, not_found, forbidden, restricted_api_key, rate_limit_exceeded, sending_paused, all_recipients_suppressed, …)
getMessage() — a human-readable description; it may change, so don't parse it
getStatusCode() — the HTTP status, or null when the request never reached the API (a client-side or transport failure)
getRetryAfter() — the response's retry-after header in seconds, or null when it has none
try {
ms.emails().get(id);
} catch (MillionSendException e) {
if ("not_found".equals(e.getName())) {
// …
}
}
POST /emails and POST /emails/batch answer 422 all_recipients_suppressed
("All recipients are suppressed") when every to recipient is on the
suppression list or opted out of the send's topicId. From v0.15.1, when the
suppression list alone empties to, POST /emails also keeps a metadata-only
suppressed email that fires email.suppressed; each retry keeps another, even with
the same Idempotency-Key (a refused send doesn't hold its key).
Inboxes add inbox_send_policy (403, a send the inbox's rules or the shared domain
refuse; one they hold is a 202 draft), inbox_send_cap_reached (429, the inbox's daily
cap, or the team's on the shared domain), approval_required (403), email_quarantined
(403), email_blocked (410, a download of an email that carries malware), too_many_waits
(429, too many concurrent long-polls) and name_review_unavailable (503, a name on the
shared domain can't be reviewed right now). A quarantine release adds trust_not_allowed
(403) and held_by_screening (409), see Inboxes; it answers forbidden (403)
and screening_unavailable (503) only to MCP clients.
An unknown route answers 404 not_found. Servers before v0.13.0 answer it in plain
text, which surfaces as application_error with status 404.
Retries
The SDK never retries on its own. Worth retrying after getRetryAfter() seconds:
503 name_review_unavailable, 429 too_many_waits, and 429 rate_limit_exceeded when
getRetryAfter() is 60 or less (the per-minute limits). Not worth retrying in the same
process: 429 inbox_send_cap_reached and the daily name-review budget (also
rate_limit_exceeded, with a getRetryAfter() that runs to the next UTC midnight), and
any 403, 404, 409 or 422.
Retry a send with the same idempotency key (emails().send, batch().send, replies,
forwards, drafts().send): a batch the API already accepted gets its stored response
back, even if a recipient was refused or suppressed since. A held draft (isDraft()) is
not a failure; never send it again. inboxes().create takes no idempotency key: after a
429 or 503 nothing was created, but after a timeout a retry can create a second inbox.
With a chosen username, a 409 on the retry usually means the first attempt succeeded:
read it with inboxes().get(address).
Resources
Every request field you set reaches the wire — nothing is dropped client-side.
Fields the server does not support yet (template on send, from/replyTo/variables
on templates) are forwarded as-is and answered with a 422.
Request options
emails().send, batch().send, contacts().batch().create and the inbox
sends (receiving().reply/forward, threads().emails().reply/forward,
drafts().send) accept a RequestOptions (the same builder shape as resend-java):
import com.millionsend.core.RequestOptions;
import com.millionsend.model.BatchValidation;
RequestOptions opts = RequestOptions.builder()
.idempotencyKey("order-123") // Idempotency-Key (setIdempotencyKey also works)
.batchValidation(BatchValidation.PERMISSIVE) // x-batch-validation on the batch endpoints
.add("X-Trace", "abc") // any extra header
.build();
ms.emails().send(options, opts);
ms.emails().send(options, "order-123"); // the plain idempotency-key overload still works
Pagination
List methods take limit, after and before (keyset cursors) and return a
ListResponse (getData(), isHasMore()). Lists run newest first (MillionSend v0.13.0
and later): after takes the last id of a page and pages toward older items, before
takes the first id and pages toward newer ones, and isHasMore() refers to that
direction. Threads sort by last activity, drafts by last update, a thread's emails run
oldest first, and labels and topics are not paginated.
// Incremental sync: page toward newer items, starting from the newest id you stored
String before = newestStoredId;
while (true) {
ListResponse<Email> page = ms.emails().list(ListOptions.builder().before(before).limit(100).build());
if (page.getData().isEmpty()) break;
save(page.getData()); // newest first within the page
if (!page.isHasMore()) break;
before = page.getData().get(0).getId();
}
A cursor whose item was deleted answers 422 validation_error (404 not_found on the
inboxes() lists): start again without a cursor and stop at the ids you already have.
Servers before v0.13.0 list oldest first; walking every page with after returns every
item on both.
Emails
ms.emails().send(options); // POST /emails
ms.emails().send(options, requestOptions); // with an Idempotency-Key / extra headers
ms.emails().get(id); // GET /emails/{id} — includes a nullable score (0-10)
ms.emails().list(ListOptions.builder().limit(50).build()); // GET /emails
ms.emails().update(id, UpdateEmailOptions.builder().scheduledAt("2026-09-01T09:00:00Z").build());
ms.emails().getInsights(id); // GET /emails/{id}/insights (404 not_found until computed)
ms.emails().cancel(id); // POST /emails/{id}/cancel (scheduled only)
ms.emails().remove(id); // DELETE /emails/{id} (MillionSend extension)
Send options are camelCase and mapped to the wire: replyTo → reply_to,
scheduledAt → scheduled_at, topicId → topic_id. to/cc/bcc/replyTo
accept one or more addresses. Also available: .tag(new Tag(name, value)),
.attachment(Attachment.builder().fileName("a.pdf").content(base64).build())
(or .path(url), plus contentType/contentId), .header(name, value) and
.template(...) (forwarded; the server answers 422 — send html/text).
resend-java's addTag/addAttachment/addHeader spellings work too.
subject takes up to 998 characters. A to, cc or bcc address on a domain that
can never receive mail (example.com, .test, .invalid, .localhost, a TLD that
doesn't exist, …) answers 422 validation_error; to test sending without emailing
anyone, use success@simulator.amazonses.com. An inbox's address on the shared domain
can be the from too, under the rules in
Sending from the shared domain.
Batch
CreateBatchEmailsResponse res = ms.batch().send(List.of(a, b),
RequestOptions.builder().idempotencyKey(key).batchValidation(BatchValidation.PERMISSIVE).build());
res.getData(); // accepted ids
res.getErrors(); // permissive mode: [{ index, message }] for rejected items; null when all accepted
Strict mode (the default) rejects the whole batch on the first invalid item.
Batch items never send from the shared domain: a strict batch fails with
403 inbox_send_policy (the message starts with emails.<index>: ), a permissive one
lists the item in getErrors().
Contacts
Contacts are team-global — one list per team, no audiences.