Envoyer un e-mail
Un message maintenant ou plus tard, avec des pièces jointes, ou une centaine en une seule requête.
Envoyer un message
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 et bcc acceptent une adresse ou une liste, et une adresse est un texte comme Ada <[email protected]> ou une map avec email et name. Le corps vient de html et text, d'un template enregistré, ou d'un draftId.
Chaque envoi porte une clé d'idempotence. Le client en crée une par appel et la réutilise quand il retente : une requête retentée rejoue donc le premier message. Passez idempotencyKey dans un RequestOptions pour que cela tienne aussi d'un processus à l'autre et après un redémarrage.
Pièces jointes
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 accepte un byte[] ou un Path, que le client encode en base64, ou du texte déjà en base64. fileId désigne un fichier téléversé avec client.files().upload, et c'est ainsi qu'on envoie un fichier de plus de 5 Mo.
Envoyer plus tard
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 accepte un Instant, un instant ISO 8601 sous forme de texte ou une durée comme PT1H. Un Instant, un OffsetDateTime ou un ZonedDateTime placé n'importe où dans un corps est envoyé comme un instant ISO 8601 en UTC. Un message programmé peut être modifié ou annulé jusqu'à son départ.
Envoyer un lot
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");Un lot contient jusqu'à 100 messages et n'est jamais tout ou rien : une mauvaise adresse fait échouer son propre élément et les autres partent quand même. Vérifiez failed() et le status de chaque élément au lieu de compter sur une exception.