简体   繁体   English

SendGrid无法在Azure App Service中运行

[英]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 hadn't referenced Microsoft.Azure.WebJobs.Extensions.SendGrid. 我没有引用Microsoft.Azure.WebJobs.Extensions.SendGrid。 The solution to this is to add it from Nuget 解决方法是从Nuget添加它
  • 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: 以下是一些要检查的事项:

  1. API Key - the API key is obviously required; API密钥 - 显然需要API密钥; double check how you are populating it. 仔细检查你如何填充它。 Often times this value is set via configuration. 通常,此值通过配置设置。 Is your deployed config value the same as the value when running locally? 部署的配置值是否与本地运行时的值相同? Maybe you have a web.config transform getting in the way during deployment, or perhaps you should have an override set (say in your app's Application Settings if you're using the Web App service). 也许你在部署过程中遇到了web.config转换,或者你应该有一个覆盖设置(比如在你的应用程序的应用程序设置中,如果你正在使用Web App服务)。
  2. SendGrid Activity & Suppressions - did you check SendGrid's Activity log? SendGrid活动和抑制 - 你检查了SendGrid的活动日志吗? The problem could be getting your request to SendGrid, or it could be that SendGrid is getting your request but isn't fulfilling it. 问题可能是您收到SendGrid的请求,或者可能是SendGrid收到您的请求但未完成请求。 Check the Activity Feed and search for the particular email you are expecting (logs are searchable and are sorted by time). 检查活动源并搜索您期望的特定电子邮件(日志可搜索并按时间排序)。 If you see a log here corresponding to your email, then the issue is on the SendGrid side. 如果您在此处看到与您的电子邮件对应的日志,则问题出在SendGrid端。 There are times that an email is suppressed and it can happen for many reasons (bounced delivery, flagged as spam, or even your DNS server was unavailable for domain ownership verification). 有时候电子邮件被抑制,并且可能由于多种原因而发生(退回传递,标记为垃圾邮件,甚至您的DNS服务器无法进行域名所有权验证)。 The Logs will provide evidence if this is the case. 如果是这种情况,日志将提供证据。 You can also go directly to the Suppressions and search for the email address there. 您也可以直接转到Suppressions并在那里搜索电子邮件地址。
  3. Network Availability - if you are hosting your application using the Web App service (as it appears to be the case), then you can assume your web app can make network requests, as is obviously required to hit the SendGrid API or SMTP servers. 网络可用性 - 如果您使用Web App服务托管您的应用程序(因为它似乎是这种情况),那么您可以假设您的Web应用程序可以发出网络请求,这显然是命中SendGrid API或SMTP服务器所必需的。 However, if you deployed your application to a VM hosted in Azure, then there are things that could be getting in the way, such as firewalls. 但是,如果您将应用程序部署到Azure中托管的VM,那么可能会遇到一些问题,例如防火墙。 Log into the VM and manually confirm that you can make network requests directly to the SendGrid API. 登录VM并手动确认您可以直接向SendGrid API发出网络请求。

我有同样的问题,对我来说问题是我将SendGrid apiKey存储为项目中的环境变量,该项目使用本地主机但不是一次api托管在azure上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM