【问题标题】:Sending simple email on Azure with Sendgrid使用 Sendgrid 在 Azure 上发送简单的电子邮件
【发布时间】:2018-06-28 08:37:09
【问题描述】:

我正在尝试在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);
        }
    }
}

【问题讨论】:

  • 查看那个类 - 假设它运行了......它会运行一次电子邮件。
  • 你能检查一下响应中的错误吗?

标签: c# azure email azure-functions sendgrid


【解决方案1】:

根据您的代码示例,它会在您没有收到任何电子邮件时运行良好。

所以要注意以下几点:

1.当您创建 api 密钥时,请确保您选择了完全访问权限 2.您使用Gmail接收电子邮件,所以设置“允许不太安全的应用程序:ON”。 3.查看回复,如果StatusCodeAccepted,则表示发送成功。 然后测试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");

        }

【讨论】:

  • 非常感谢这个详细的答案,但我猜我做错了。我不能用断点停止程序,所以我看不到任何状态码 -_- 我正在挖掘网络来解决这个问题,或者你能和我分享项目包以了解我的代码有什么问题吗?再次感谢你。祝你有美好的一天
  • 您需要将Sloution Configuration 设置为Debug。这是debug的方法。
  • 一切都给 null :( 我也无法达到我的 sengridkey。我不明白为什么:/ imgur.com/gHpIn7z
  • 根据图片,将sendgridkey放在app.config或portal的appsetting中,获取key值的方式是正确的。您可以参考article 了解有关 sendgrid 的更多详细信息。冷静下来,进行故障排除。
  • 本文不解释 Azure 门户上的“应用服务”这个东西。我点击了应用服务并打开了一些功能,然后添加了 api 密钥。但是这里有很多应用程序服务。我需要选择哪一个? imgur.com/ZdcLEs1
猜你喜欢
  • 1970-01-01
  • 2016-04-23
  • 2018-11-08
  • 1970-01-01
  • 2013-08-24
  • 1970-01-01
  • 2021-10-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多