تخطَّ إلى المستندات
C#

إرسال رسالة

رسالة واحدة الآن أو لاحقًا، مع مرفقات، أو مئة في طلب واحد.

إرسال رسالة

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 عنوانًا واحدًا أو قائمة، والعنوان نص مثل Ada <[email protected]> أو قاموس فيه email وname. ويأتي المحتوى من html وtext، أو من template محفوظ، أو من draftId.

كل إرسال يحمل مفتاح عدم تكرار. ينشئ العميل واحدًا لكل استدعاء ويعيد استخدامه عند إعادة المحاولة، فيعيد الطلب المُعاد الرسالة الأولى. مرّر 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 ميغابايت.

الإرسال لاحقًا

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 في أي موضع من الجسم يُرسل لحظةً بصيغة ISO 8601 بتوقيت UTC. يمكن تغيير الرسالة المجدوَلة أو إلغاؤها إلى أن تُرسل.

إرسال دفعة

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 لكل عنصر بدل الاعتماد على استثناء.