Belgelere geç
C#

E-posta gönderme

Şimdi ya da daha sonra tek bir ileti, eklerle birlikte, ya da tek istekte yüz ileti.

Bir ileti gönderme

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 ve bcc tek bir adres ya da bir liste alır; adres ise Ada <[email protected]> gibi bir metin ya da email ve name içeren bir sözlüktür. Gövde html ve text alanlarından, kayıtlı bir template değerinden ya da bir draftId değerinden gelir.

Her gönderim bir idempotency anahtarı taşır. İstemci her çağrı için bir tane üretir ve yeniden denerken onu yeniden kullanır, böylece yeniden denenen bir istek ilk iletiyi yineler. Bunun süreçler ve yeniden başlatmalar arasında da geçerli olması için idempotencyKey: geçirin.

Ekler

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, istemcinin base64 olarak kodladığı bir byte[] ya da okunabilir bir Stream alır, ya da zaten base64 olan bir metin alır. fileId, client.Files.UploadAsync() ile yüklenmiş bir dosyayı adlandırır; 5 MB'tan büyük bir dosya bu şekilde gönderilir.

Daha sonra gönderme

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 bir DateTimeOffset, bir ISO 8601 anı ya da PT1H gibi bir süre alır. Gövdenin herhangi bir yerindeki bir DateTimeOffset, UTC olarak bir ISO 8601 anı biçiminde gönderilir. Zamanlanmış bir ileti gidene kadar değiştirilebilir ya da iptal edilebilir.

Toplu gönderim

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"]}");    }}

Bir toplu gönderim en çok 100 ileti alır ve asla ya hep ya hiç değildir: hatalı bir adres yalnızca kendi öğesini başarısız kılar, geri kalanı yine de gider. Bir özel duruma güvenmek yerine Failed değerini ve her öğenin status değerini denetleyin.