Zur Dokumentation springen
Java

Eine E-Mail senden

Eine Nachricht jetzt oder später, mit Anhängen, oder hundert in einer Anfrage.

Eine Nachricht senden

Send.java
Map<String, Object> email = client.emails().send(Body.of(    "from", "Acme Billing <[email protected]>",    "to", List.of("[email protected]", Body.of("email", "[email protected]", "name", "Grace")),    "replyTo", "[email protected]",    "subject", "Your September invoice",    "html", "<p>Invoice attached.</p>",    "text", "Invoice attached."), RequestOptions.create().idempotencyKey("invoice-2026-09-ada")); System.out.println(email.get("id") + " " + email.get("status"));

to, cc und bcc nehmen eine Adresse oder eine Liste, und eine Adresse ist Text wie Ada <[email protected]> oder eine Map mit email und name. Der Körper kommt aus html und text, aus einem gespeicherten template oder aus einer draftId.

Jeder Versand trägt einen Idempotenzschlüssel. Der Client erzeugt einen pro Aufruf und verwendet ihn bei Wiederholungen erneut, sodass eine wiederholte Anfrage die erste Nachricht erneut ausgibt. Übergeben Sie idempotencyKey in einem RequestOptions, damit das auch über Prozesse und Neustarts hinweg gilt.

Anhänge

Attachments.java
byte[] report = "month,total".getBytes(StandardCharsets.UTF_8); client.emails().send(Body.of(    "from", "[email protected]",    "to", "[email protected]",    "subject", "Your September report",    "text", "The report is attached.",    "attachments", List.of(        Body.of("filename", "report.csv", "content", report, "contentType", "text/csv"),        Body.of("fileId", "b3e1f0a4-6c2d-4e8a-9f17-2d5c8a0b4e6f")    )));

content nimmt ein byte[] oder einen Path, die der Client als Base64 kodiert, oder Text, der bereits Base64 ist. fileId benennt eine Datei, die mit client.files().upload hochgeladen wurde, und so wird eine Datei über 5 MB gesendet.

Später senden

Schedule.java
Map<String, Object> scheduled = client.emails().send(Body.of(    "from", "[email protected]",    "to", "[email protected]",    "subject", "Your invoice is due tomorrow",    "text", "A reminder that your invoice is due tomorrow.",    "scheduledAt", Instant.now().plus(Duration.ofHours(1)))); String id = (String) scheduled.get("id"); client.emails().reschedule(id, Instant.now().plus(Duration.ofHours(2)));client.emails().cancel(id);

scheduledAt nimmt einen Instant, einen ISO-8601-Zeitpunkt als Text oder eine Dauer wie PT1H. Ein Instant, ein OffsetDateTime oder ein ZonedDateTime an beliebiger Stelle in einem Körper wird als ISO-8601-Zeitpunkt in UTC gesendet. Eine geplante Nachricht kann geändert oder storniert werden, bis sie rausgeht.

Einen Batch senden

Batch.java
BatchResult batch = client.emails().sendBatch(List.of(    Body.of("from", "[email protected]", "to", "[email protected]", "subject", "Your invoice", "text", "Invoice attached."),    Body.of("from", "[email protected]", "to", "[email protected]", "subject", "Your invoice", "text", "Invoice attached."))); for (Map<String, Object> item : batch) {    if ("error".equals(item.get("status"))) {        System.err.println(item.get("index") + " " + item.get("error"));    }} System.out.println(batch.sent() + " sent, " + batch.failed() + " failed");

Ein Stapel fasst bis zu 100 Nachrichten und ist nie alles oder nichts: Eine fehlerhafte Adresse lässt nur ihren eigenen Eintrag scheitern, und der Rest geht trotzdem raus. Prüfen Sie failed() und den status jedes Eintrags, statt sich auf eine Exception zu verlassen.