E-posta gönderme
Şimdi ya da daha sonra tek bir ileti, eklerle birlikte, ya da tek istekte yüz ileti.
Bir ileti gönderme
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 ve bcc tek bir adres ya da bir liste alır; adres ise Ada <[email protected]> gibi bir metin ya da email ve name içeren bir map'tir. Gövde html ve text alanlarından, kayıtlı bir template değerinden ya da bir draftId değerinden gelir.
Her gönderim bir idempotency anahtarı taşır. İstemci her çağrı için bir tane üretir ve yeniden denerken onu yeniden kullanır, böylece yeniden denenen bir istek ilk iletiyi yineler. Bunun süreçler ve yeniden başlatmalar arasında da geçerli olması için bir RequestOptions içinde idempotencyKey geçirin.
Ekler
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, istemcinin base64 olarak kodladığı bir byte[] ya da bir Path veya zaten base64 olan bir metin alır. fileId, client.files().upload ile yüklenmiş bir dosyayı adlandırır; 5 MB'tan büyük bir dosya bu şekilde gönderilir.
Daha sonra gönderme
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 bir Instant, metin olarak bir ISO 8601 anı ya da PT1H gibi bir süre alır. Bir gövdenin herhangi bir yerindeki Instant, OffsetDateTime ya da ZonedDateTime, UTC'de bir ISO 8601 anı olarak gönderilir. Zamanlanmış bir ileti, gidene kadar değiştirilebilir ya da iptal edilebilir.
Toplu gönderim
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");Bir toplu gönderim en çok 100 ileti alır ve asla ya hep ya hiç değildir: hatalı bir adres yalnızca kendi öğesini başarısız kılar, geri kalanlar yine gider. Bir istisnaya güvenmek yerine failed() değerini ve her öğenin status alanını denetleyin.