How To Send Email Using Sendgrid With Azure

Having an effective and dependable email delivery system is vital in today’s society. Microsoft Azure, in collaboration with SendGrid, provides exactly that. Microsoft Azure is a cloud service developed by Microsoft, while SendGrid is a cloud-based email delivery platform. Through the integration of these two services, you can easily send emails through your application. This blog will guide you on how to use SendGrid with Azure to send emails.

Prerequisites

  • Microsoft Azure Account
  • SendGrid Account
  • Basic Understanding of Using Azure

Step 1: Create a SendGrid Account

You will need a SendGrid account to start sending emails. You can sign up for a free account, which allows for a certain number of emails per day. Once you have an account and you are logged in, you can generate an API key. The API key will be used to authenticate your requests.

Step 2: Create an Azure Function

Log in to your Azure portal and create a new function app. A function app in Azure is a solution for running small pieces of code, or “functions,” in the cloud. You can write just the code you need for the problem at hand, without worrying about a whole application or the infrastructure to run it.

Step 3: Install SendGrid NuGet package

In your function app, go to the ‘Platform features’ tab and open the ‘Advanced tools (Kudu)’. Go to ‘Debug console’ > ‘CMD’ > ‘Site’ > ‘wwwroot’ and create a new file named ‘function.proj’. Edit this file and add the following lines:

    <project sdk="Microsoft.NET.Sdk">
        <propertygroup>
            <targetframework>netcoreapp3.1</targetframework>
        </propertygroup>
        <itemgroup>
            <packagereference include="SendGrid" version="9.21.1"></packagereference>
        </itemgroup>
    </project>
    

Save and close the file. The function app will automatically restore the NuGet package.

Step 4: Write the Function

Go back to the Azure portal and open your function. Add a new function and choose the HTTP trigger template, name the function, and set the authorization level to function.

Now we will write our function. Here is a simple example:

    #r "SendGrid"
    using SendGrid.Helpers.Mail;
    using Microsoft.Azure.WebJobs.Extensions.SendGrid;

    public static class SendGridFunction
    {
        [FunctionName("SendGridFunction")]
        public static void Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            [SendGrid(ApiKey = "Your-Api-Key")] out SendGridMessage message)
        {
            message = new SendGridMessage();
            message.AddTo("[email protected]");
            message.AddContent(MimeType.Text, "Hello, world!");
            message.SetFrom(new EmailAddress("[email protected]"));
            message.SetSubject("Azure + SendGrid test");
        }
    }
    

Replace “Your-Api-Key” with the API key from your SendGrid account. Replace the ‘to’ and ‘from’ email addresses with valid values.

Step 5: Test the Function

Now that everything is set up, you can test the function. In the Azure portal, navigate to your function and click on ‘Test/Run’. Enter a request body if your function requires it, and hit ‘Run’. You should receive an email at the address specified in your function.

That’s it! You have successfully sent an email using SendGrid with Azure. The combination of Azure and SendGrid provides a powerful, scalable, and reliable email solution for your applications.