Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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("[email protected]", "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?

like image 989
severin Avatar asked Aug 20 '16 10:08

severin


People also ask

How do I connect to Azure SendGrid?

If you are using Microsoft's cloud platform, you can easily integrate with SendGrid. Simply navigate to the Azure Marketplace, locate the SendGrid add-on, select the appropriate plan, and get ready to start sending.

Is SendGrid free in Azure?

If SendGrid is deployed from Azure, there is no free plan. The cheapest paid plan (Bronze) starts with 40,000 emails per month with a $9.95 per month fee.


1 Answers

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("[email protected]");
        string subject = "Test";
        Email to = new Email("[email protected]");
    
        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();
}
like image 160
Matt Avatar answered Sep 18 '22 11:09

Matt