إرسال رسالة
رسالة واحدة الآن أو لاحقًا، مع مرفقات، أو مئة في طلب واحد.
إرسال رسالة
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. ويأتي الجسم من 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 لكل عنصر بدل الاعتماد على استثناء.