ارسال یک ایمیل
یک پیام، اکنون یا بعداً، با پیوست، یا صد پیام در یک درخواست.
فرستادن یک پیام
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 مگابایت همینطور فرستاده میشود.
فرستادن در آینده
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 در هر جای بدنه بهصورت یک لحظهٔ ISO 8601 در UTC فرستاده میشود. پیام زمانبندیشده را میتوان تا پیش از رفتنش تغییر داد یا لغو کرد.
ارسال دستهای
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 هر مورد را بررسی کنید.