简体   繁体   中英

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:

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.

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. The solution to this is to add it from Nuget
  • I wasn't waiting for the async send to complete. Add .Wait() to the async function that sends the email. The full code I used for program.cs is below:

     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:

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. I can send when debugging locally AND after deployed to production in Azure. Here are a couple of things to check:

  1. API Key - the API key is obviously required; 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).
  2. SendGrid Activity & Suppressions - did you check SendGrid's Activity log? The problem could be getting your request to SendGrid, or it could be that SendGrid is getting your request but isn't fulfilling it. 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. 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). 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.
  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. 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. Log into the VM and manually confirm that you can make network requests directly to the SendGrid API.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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