Sending simple email on Azure with Sendgrid(使用SendGrid在Azure上发送简单的电子邮件)
本文介绍了使用SendGrid在Azure上发送简单的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在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
被接受表示成功发送电子邮件。
然后重新测试,它可能工作得很好。
此外,您还希望创建一个每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");
}
这篇关于使用SendGrid在Azure上发送简单的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:使用SendGrid在Azure上发送简单的电子邮件


猜你喜欢
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 输入按键事件处理程序 2022-01-01