ارسال یک ایمیل
یک پیام، اکنون یا بعداً، با پیوست، یا صد پیام در یک درخواست.
فرستادن یک پیام
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]> یا یک map با email و name است. بدنه از html و text، از یک template ذخیرهشده یا از یک draftId میآید.
هر ارسال یک کلید یکتایی دارد. کلاینت برای هر فراخوانی یکی میسازد و هنگام تلاش دوباره همان را به کار میبرد، پس درخواستِ دوبارهامتحانشده پیام نخست را بازپخش میکند. idempotencyKey را در یک RequestOptions بدهید تا این میان فرایندها و راهاندازیهای دوباره هم برقرار بماند.
پیوستها
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 مگابایت همینطور فرستاده میشود.
فرستادن در آینده
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 در هر جای بدنه به شکل یک لحظهٔ ISO 8601 در UTC فرستاده میشود. پیام زمانبندیشده را تا پیش از رفتن میتوان تغییر داد یا لغو کرد.
ارسال دستهای
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 هر آیتم را بررسی کنید.