ईमेल भेजें
एक संदेश अभी या बाद में, अटैचमेंट के साथ, या एक ही अनुरोध में सौ संदेश।
एक संदेश भेजें
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 और bcc एक पता या एक सूची लेते हैं, और पता Ada <[email protected]> जैसा टेक्स्ट होता है या email और name वाला map। बॉडी html और text से, किसी सहेजे गए template से या किसी draftId से आती है।
हर भेजने वाली कॉल एक आइडेम्पोटेंसी कुंजी रखती है। क्लाइंट हर कॉल के लिए एक बनाता है और रीट्राई में उसी को दोबारा इस्तेमाल करता है, इसलिए दोबारा आज़माया गया अनुरोध पहला संदेश दोहराता है। इसे प्रोसेस और रीस्टार्ट के पार भी बनाए रखने के लिए एक RequestOptions में idempotencyKey पास करें।
Attachments
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 एक byte[] या एक Path लेता है, जिसे क्लाइंट base64 में एन्कोड करता है, या ऐसा टेक्स्ट जो पहले से base64 है। fileId उस फ़ाइल का नाम देता है जो client.files().upload से अपलोड की गई है, और 5 MB से बड़ी फ़ाइल इसी तरह भेजी जाती है।
बाद में भेजें
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 एक Instant, टेक्स्ट के रूप में एक ISO 8601 क्षण या PT1H जैसी अवधि लेता है। बॉडी में कहीं भी मौजूद Instant, OffsetDateTime या ZonedDateTime को UTC में एक ISO 8601 क्षण के रूप में भेजा जाता है। शेड्यूल किया गया संदेश जाने तक बदला या रद्द किया जा सकता है।
batch भेजें
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");एक बैच में 100 तक संदेश आते हैं और यह कभी सब या कुछ नहीं वाला नहीं होता: गलत पता केवल अपने आइटम को विफल करता है और बाकी फिर भी जाते हैं। किसी एक्सेप्शन पर निर्भर रहने के बजाय failed() और हर आइटम का status जाँचें।