SendGrid does not work in Azure App Service

I use the latest SendGrid.NET package (8.0.3) to send email from my ASP.NET Core web application:

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 runs on an Azure application service instance, mail fails.

The code works fine without any exceptions, so I don’t need much to work, but I think it might be some kind of problem with the firewall.

Has anyone encountered similar problems? How can I do it?

+5
source share
3 answers

I am using SendGrid in my Azure Web App. I can send when debugging locally and after deployment in Azure. Here are a few things to check:

  • API key - obviously, an API key is required; double check how you fill it out. Often this value is set through configuration. Is the configuration value you set the same as the startup value locally? Perhaps you have a web.config conversion in the path during deployment, or maybe you should have a set of overrides (say, in the Application Settings application, if you use the web application service).
  • SendGrid Actions and Suppresses - Have you checked the SendGrid activity log? The problem may be getting your request to SendGrid, or it may be that SendGrid is receiving your request but not executing it. Check the activity feed and find the specific email address that you expect (logs are searchable and sorted by time). If you see here a journal corresponding to your letter, the problem arises on the side of SendGrid. There are times when an email is suppressed, and this can happen for many reasons (scanned delivery marked as spam or even your DNS server was not available for checking domain rights). Magazines will provide evidence if so. You can also go directly to suppressions and find the email address there.
  • Network availability. If you host your application using the web application service (which seems to be the case), you can assume that your web application can perform network requests, since you obviously need to use the SendGrid API or the SMTP server. However, if you deployed your application to a virtual machine hosted on Azure, then there are things that can interfere, such as firewalls. Log in to the virtual machine and manually confirm that network requests can be sent directly to the SendGrid API.
0
source

I had the same problem and the problem for me was that I had the SendGrid apiKey attribute, which is stored as an environment variable in the project, which worked with the local host, but not once, when the api was placed on the azure server.

0
source

Although the question is old, I provide an answer when I come across a similar problem and could not find any clear solutions. The reason he didn't work for me was because

  • I did not refer to Microsoft.Azure.WebJobs.Extensions.SendGrid. The solution to this is to add it from Nuget
  • I did not expect the completion of sending async. Add .Wait () to the async function that sends the email. The full code that 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 suggest adding .Wait () to the function call:

 public Task SendEmailAsync(string email, string subject, string message) { //Added .Wait() return Send(email, subject, message).Wait(); } 
0
source

All Articles