Sending emails from your application with easy and secure way using Azure Sendgrid service

Sending emails from your application with easy and secure way using Azure Sendgrid service

Using .NET core on Azure

Sending emails from your application is common requirement from any kind of application. for example send daily reports, notifications, monitoring alerts, information, promotions, announcements etc. when we try to integrate email service into project we mainly focus on Cost, Features and Security.

A few years back, I create one scheduler using Java multi-threading for sending daily reports via application and that was a very hectic task of setting up our own email server, calculate waiting time, read credentials from config files and send email. Also, there are lots of security concerns when we need to configure credentials (Email Address and Password) in property files.

Also latest applications hosted on different cloud services and it is another challenge to manage security when using third party services to send emails. the best choice is using cloud provided services for email communication that will provide efficient and secure way.

In my latest requirement I have used the Sendgrid service in our cloud base application for easy and secure mail communication without adding sensitive data in configuration files.

As part of this configuration you just need to provide an API key (Which you can create an inside Sendgrid resource). Instead of adding email address and password you just provide an API key to your application when making SendGrid email requests in your code. If you have key-vault service to your application, then your application can read that API key from key-vault which is strong security mechanism.

Sendgrid is a partner service on cloud so it is available on all other cloud services as well like AWS (Pricing tire may be different).

Microsoft azure offers free package with 25,000 free emails each month.

Integration on Microsoft Azure Cloud

Deploy and configuring SendGrid was very easy on Azure with just few steps with online available documentation with example .

here, you need to follow some easy steps:

  1. Deploy SendGrid service using documentation.
  2. Create your account inside SendGrid service and configure your credentials here
  3. Generate API Key with simple steps and copy that key in your key-vault/config file.

Easy Development

SendGrid is a service made by Twilio which provide different libraries for different platforms, so you can choose any programming language or you can integrate this services on any existing development platform of your product e.g Java, .Net, NodeJS etc.

To integrate application with SendGrid service which deployed on your azure portal download and install the Azure SendGrid NuGet package. Once installed, you can start to send email by few lines.

Sample code for using the Azure SendGrid email service you can find here ,

Along with C#, SendGrid supports below programming languages, you can choose one of them.


C# Code sample

static async Task execute(ISendGridClient client)
    var from = new EmailAddress(Configuration.GetValue("SendGrid:From", ""), "Example User");
    var to = new EmailAddress(Configuration.GetValue("SendGrid:To", ""), "Example User");
    var msg = new SendGridMessage
        From = from,
        Subject = "Sending with Twilio SendGrid is Fun"
    msg.AddContent(MimeType.Text, "and easy to do anywhere, even with C#");
    if (Configuration.GetValue("SendGrid:SandboxMode", false))
        msg.MailSettings = new MailSettings
            SandboxMode = new SandboxMode
                Enable = true
    Console.WriteLine($"Sending email with payload: \n{msg.Serialize()}");
    var response = await client.SendEmailAsync(msg).ConfigureAwait(false);

    Console.WriteLine($"Response: {response.StatusCode}");

Thank You !!!