C#
이메일 보내기
지금 또는 나중에 한 통, 첨부 파일과 함께, 또는 한 번의 요청으로 백 통.
메시지 보내기
var sent = await client.Emails.SendAsync(new Body{ ["from"] = "Acme Billing <[email protected]>", ["to"] = new[] { "[email protected]", "[email protected]" }, ["replyTo"] = "[email protected]", ["subject"] = "Your September invoice", ["html"] = "<p>The invoice is attached.</p>", ["text"] = "The invoice is attached.", ["tags"] = new Body { ["invoice"] = "inv_2026_09_4192" },}); Console.WriteLine($"{sent["id"]} {sent["status"]}");to, cc, bcc는 주소 하나 또는 목록을 받습니다. 주소는 Ada <[email protected]> 같은 텍스트이거나 email과 name이 있는 딕셔너리입니다. 본문은 html과 text, 저장된 template, 또는 draftId에서 만들어집니다.
모든 발송은 멱등성 키를 가집니다. 클라이언트는 호출마다 키를 하나 만들고 재시도할 때 다시 쓰므로, 재시도된 요청은 첫 메시지를 재생합니다. 프로세스와 재시작을 넘어서도 보장하려면 idempotencyKey:를 전달하세요.
첨부 파일
await using var invoice = File.OpenRead("invoice.pdf"); await client.Emails.SendAsync(new Body{ ["from"] = "[email protected]", ["to"] = "[email protected]", ["subject"] = "Your September invoice", ["text"] = "The invoice is attached.", ["attachments"] = new[] { new Body { ["filename"] = "invoice.pdf", ["content"] = invoice, ["contentType"] = "application/pdf" }, new Body { ["fileId"] = "file_3f9c2a7b1e4d8f60a5c7b92d" }, },});content는 byte[] 또는 읽을 수 있는 Stream을 받고, 클라이언트가 base64로 인코딩합니다. 이미 base64인 텍스트도 받을 수 있습니다. fileId는 client.Files.UploadAsync()로 올린 파일을 가리키며, 5 MB보다 큰 파일은 이렇게 보냅니다.
나중에 보내기
var scheduled = await client.Emails.SendAsync(new Body{ ["from"] = "[email protected]", ["to"] = "[email protected]", ["subject"] = "Your invoice is due tomorrow", ["text"] = "A reminder that the invoice is due tomorrow.", ["scheduledAt"] = DateTimeOffset.UtcNow.AddDays(1),}); var id = scheduled["id"]!.GetValue<string>(); await client.Emails.RescheduleAsync(id, DateTimeOffset.UtcNow.AddDays(2)); var cancelled = await client.Emails.CancelAsync(id); Console.WriteLine(cancelled["status"]);scheduledAt은 DateTimeOffset, ISO 8601 시각, 또는 PT1H 같은 기간을 받습니다. 본문 어디에 있든 DateTimeOffset은 UTC의 ISO 8601 시각으로 전송됩니다. 예약된 메시지는 발송되기 전까지 변경하거나 취소할 수 있습니다.
배치 발송
var result = await client.Emails.SendBatchAsync(new[]{ new Body { ["from"] = "[email protected]", ["to"] = "[email protected]", ["subject"] = "Order AC-4192 has shipped", ["text"] = "It is on its way." }, new Body { ["from"] = "[email protected]", ["to"] = "[email protected]", ["subject"] = "Order AC-4193 has shipped", ["text"] = "It is on its way." },}); Console.WriteLine($"{result.Sent} sent, {result.Failed} failed"); foreach (var item in result){ if ((string?)item["status"] == "error") { Console.WriteLine($"{item["index"]} {item["error"]?["message"]}"); }}일괄 발송은 최대 100통의 메시지를 담으며, 전부 아니면 전무가 아닙니다. 잘못된 주소는 해당 항목만 실패시키고 나머지는 그대로 발송됩니다. 예외에 의존하지 말고 Failed와 각 항목의 status를 확인하세요.