[英]SendGrid not working from Azure App Service
I am using the latest SendGrid .NET package (8.0.3) to send e-mails from my ASP.NET Core web app: 我使用最新的SendGrid .NET包(8.0.3)从我的ASP.NET Core Web应用程序发送电子邮件:
public Task SendEmailAsync(string email, string subject, string message)
{
return Send(email, subject, message);
}
async Task Send(string email, string subject, string message)
{
dynamic sg = new SendGridAPIClient(_apiKey);
var from = new Email("noreply@my.domain", "My Name");
var to = new Email(email);
var content = new Content("text/html", message);
var mail = new Mail(from, subject, to, content);
await sg.client.mail.send.post(requestBody: mail.Get());
}
It works locally, but running on an Azure App Service instance the mails don't come through. 它在本地工作,但在Azure App Service实例上运行时,邮件无法通过。
The code runs fine without any exceptions so I have little to work with, but I am thinking it could be some sort of firewall issue. 代码运行良好,没有任何例外,所以我几乎无法使用,但我认为它可能是某种防火墙问题。
Has anyone experienced similar issues? 有没有人遇到类似的问题? How do I go about to debug this?
我该如何调试呢?
Although the question is old I'm providing an answer as I came across a similar issue and couldn't find any clear solutions. 虽然问题很老,但我提供了答案,因为我遇到了类似的问题,找不到任何明确的解决方案。 The reason why it wasn't working for me was because
它不适合我的原因是因为
I wasn't waiting for the async send to complete. 我不是在等待异步发送完成。 Add .Wait() to the async function that sends the email.
将.Wait()添加到发送电子邮件的异步函数中。 The full code I used for program.cs is below:
我用于program.cs的完整代码如下:
static void Main() { var config = new JobHostConfiguration(); if (config.IsDevelopment) { config.UseDevelopmentSettings(); } config.UseSendGrid(); //The function below has to wait in order for the email to be sent SendEmail(*Your SendGrid API key*).Wait(); } public static async Task SendEmail(string key) { dynamic sg = new SendGridAPIClient(key); Email from = new Email("from@domain.com"); string subject = "Test"; Email to = new Email("to@domain.com"); Content content = new Content("text/html", "CONTENT HERE"); Mail mail = new Mail(from, subject, to, content); dynamic s = await sg.client.mail.send.post(requestBody: mail.Get()); }
So I would suggest you add .Wait() to your function call: 所以我建议你在你的函数调用中添加.Wait():
public Task SendEmailAsync(string email, string subject, string message)
{
//Added .Wait()
return Send(email, subject, message).Wait();
}
I use SendGrid in my Azure Web App as well. 我也在Azure Web App中使用SendGrid。 I can send when debugging locally AND after deployed to production in Azure.
我可以在本地调试时发送,也可以在Azure中部署到生产后发送。 Here are a couple of things to check:
以下是一些要检查的事项:
我有同样的问题,对我来说问题是我将SendGrid apiKey存储为项目中的环境变量,该项目使用本地主机但不是一次api托管在azure上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.