簡體   English   中英

使用Sendgrid在Azure上發送簡單電子郵件

[英]Sending simple email on Azure with Sendgrid

我正在嘗試使用Azure SendGrid發送電子郵件。 我遵循了以下說明: https : //docs.microsoft.com/tr-tr/azure/sendgrid-dotnet-how-to-send-email 但它似乎不起作用。 通過smtp發送雖然可以。

using System;
using System.Threading.Tasks;
using SendGrid;
using SendGrid.Helpers.Mail;

namespace Example
{
    internal class Example
    {
        private static void Main()
        {
            Execute().Wait();
        }

        static async Task Execute()
        {
            var apiKey = System.Environment.GetEnvironmentVariable("SENDGRID_APIKEY");
            var client = new SendGridClient(apiKey);
            var msg = new SendGridMessage()
            {
                From = new EmailAddress("from@hotmail.com", "DX Team"),
                Subject = "Hello World from the SendGrid CSharp SDK!",
                PlainTextContent = "Hello, Email!",
                HtmlContent = "<strong>Hello, Email!</strong>"
            };
            msg.AddTo(new EmailAddress("to@gmail.com", "Test User"));
            var response = await client.SendEmailAsync(msg);
        }
    }
}

根據您的代碼示例,當您沒有收到任何電子郵件時,它將運行良好。

因此,請注意以下幾點:

1.創建api密鑰時,請確保選擇“ 完全訪問權限” 在此處輸入圖片說明 2.您使用Gmail接收電子郵件,因此設置“ 允許安全性較低的應用程序:開啟 ”。 在此處輸入圖片說明 3.檢查響應,如果StatusCode接受,則表示已成功發送電子郵件。 在此處輸入圖片說明 然后測試agian,它可能會很好地工作。

另外,您想創建一個每15分鍾發送一次電子郵件的功能。

您可以參考以下代碼,我在v1中創建了azure函數。

public static void Run([TimerTrigger("0 */15 * * * *")]TimerInfo myTimer, [SendGrid(ApiKey = "sendgridkey")] out SendGridMessage message, TraceWriter log)
        {
            log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
            message = new SendGridMessage();
            message.AddTo("testto@gmail.com");
            message.AddContent("text/html", "Test body");
            message.SetFrom(new EmailAddress("testfrom@gmail.com"));
            message.SetSubject("Subject");

        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM