# Send Emails through Azure with C# and SendGrid

In this post, I'll walk through the process that I completed to create an account with SendGrid and send an email with C#.

Go to the Azure Portal and search services for SendGrid and create an account as shown below. You'll notice that I used the Free account as it is good enough for what I was trying to accomplish.

Go to your SendGrid account once provisioned and click on Manage and it will bring you to https://app.sendgrid.com/ (opens new window) as shown below.

From the SendGrid portal, you are going to want to grab your API key. You can find it under Settings, then API Keys

Give your API Key a name and then give it Full Access and click Create and View.

Remember this! Copy this key somewhere safe as we'll be using it again shortly!

Now that you have your API Key, open Visual Studio and create a new Console Application. (Keep in mind this could be an Azure Function for example). You'll need to add in the Sendgrid NuGet package (which you can do from Manage NuGet packages).

Install the NuGet package and copy and paste the following C# code into your Console application, simply replacing your API key.

    class Program
    {
        static void Main(string[] args)
        {
            Execute().Wait();

        }
        static async Task Execute()
        {
            var client = new SendGridClient("ENTER-YOUR-API-KEY-HERE");
            var msg = new SendGridMessage();

            msg.SetFrom(new EmailAddress("a@gmail.com", "Azure Tips and Tricks"));

            var recipients = new List<EmailAddress>
                {
                    new EmailAddress("a@gmail.com"),
                    new EmailAddress("b@gmail.com"),
                    new EmailAddress("c@gmail.com")
                };
            msg.AddTos(recipients);

            msg.SetSubject("Mail from Azure and SendGrid");

            msg.AddContent(MimeType.Text, "This is just a simple test message!");
            msg.AddContent(MimeType.Html, "<p>This is just a simple test message!</p>");
            var response = await client.SendEmailAsync(msg);

        }
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

Run the application and check whatever email that you sent it to!