ドキュメント本文へスキップ
C#

メールを送信する

今すぐ、またはあとで 1 通。添付ファイル付きでも、1 回のリクエストで 100 通でも送れます。

メッセージを送信する

send.cs
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 は 1 つのアドレスまたはリストを受け取ります。アドレスは Ada <[email protected]> のようなテキストか、email と name を持つディクショナリです。本文は html と text、保存済みの template、または draftId から作られます。

すべての送信は冪等性キーを持ちます。クライアントは呼び出しごとにキーを 1 つ作り、再試行でも同じキーを使うため、再試行されたリクエストは最初のメッセージを再生します。プロセスや再起動をまたいでも保証するには idempotencyKey: を渡してください。

添付ファイル

attachments.cs
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 を超えるファイルはこの方法で送ります。

あとで送信する

schedule.cs
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 日時として送信されます。スケジュールされたメッセージは、送信されるまで変更またはキャンセルできます。

バッチ送信する

batch.cs
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 を確認してください。