이메일 보내기
지금 또는 나중에 한 통, 첨부 파일과 함께, 또는 한 번의 요청으로 백 통.
메시지 보내기
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를 전달하세요.
첨부 파일
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는 클라이언트가 base64로 인코딩하는 byte[]나 Path, 또는 이미 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 시각으로 전송됩니다. 예약된 메시지는 나가기 전까지 변경하거나 취소할 수 있습니다.
배치 발송
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를 확인하세요.