ईमेल भेजें
एक संदेश अभी या बाद में, अटैचमेंट के साथ, या एक ही अनुरोध में सौ संदेश।
एक संदेश भेजें
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
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 समय-बिंदु के रूप में भेजा जाता है। शेड्यूल किया गया संदेश जाने तक बदला या रद्द किया जा सकता है।
batch भेजें
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 जाँचें।