Using Microsoft Azure SendGrid to launch email campaigns

In the past, before the cloud era, we had on-premises servers and email servers configured to send emails outside of the organization, and in several cases, the same infrastructure was used to send campaign emails to the customers. Time has changed, and now that for many organizations Exchange has been replaced by Office 365, it is easier to find a unicorn in the wild than a relay server lying around in your ever smaller on-premises environment. Microsoft Azure offers a service called SendGrid, and we can have up to two SendGrid accounts per Azure subscription. In short, we can configure our applications to use that new service and get rid of any local relay server and integrations with Office 365 to send campaign messages. (It is possible to relay with Office 365, but by using an Azure SendGrid we have better control of the outbound mail flow.)

SendGrid be implemented with small changes in your application code, or if you do have some time, you can always leverage Web API to send emails. If you don’t want to change your code, use the SMTP option and change the server and authentication. The service supports Web API and secure SMTP connections.

Creating a SendGrid account

The first step on our journey is to create a new SendGrid account. Logged on the Azure Portal, search for SendGrid and click on SendGrid Accounts. In the new blade click on Create a New SendGrid Account. The new blade will require a name and a password to be defined, subscription, resource group, and a pricing tier. The pricing tier is related to your ability to send an email every month. The free tier allows up to 25,000 emails, just to give the reader an idea. We also need to fill out the Contact Information and Legal Terms sections. Make sure to fill out all the information.

The provision of the SendGrid account may take a few minutes, and the result will be a new blade with some information about the current account including location, resource group, and pricing information.

In the right side of the Essentials title, we have some resources that help the administrator to retrieve and use some necessary information about a SendGrid Account, for example, the SMTP server that our applications can use (smtp.sendgrid.net).

The SendGrid feature has some visibility on the Azure Portal. However, the feature has a complete set of settings, templates, reports, and you name it available in its own portal. The best way to access it is by clicking on the Manage icon located on the left side right underneath the SendGrid Account name.

Using the SendGrid portal and sending the first email

The SendGrid portal has a comprehensive set of tools and documentation to support your requirements to send email using several interfaces, languages, and systems.

When opening for the portal (app.sendgrid.com) for first time, a page asks for your email that will be confirmed later on. Make sure to check your Inbox and confirm the receiving of that first email.

The best way to explore the interface is going through the process to send your first email using the feature. To get started, click on your name on the upper left corner, and then click on Setup Guide (Item 1, in the picture). The dashboard allows us to choose between using Web API or SMTP Relay, or even integration with existent web applications (WordPress, Drupal, and many more).

In this article, I’m writing from a Mac (trying to be cool among the Starbucks crowd), so I will use cURL language, but if you feel more comfortable writing the code in C#, Java, please go for it. So, my choice was Integrate using our Web API or SMTP Relay, click Start on that section.

In the Overview page, we will be selecting Web API, and then click on Choose.

In the Integrate page, the wizard helps a lot. Pick your language, and all the code required to send your first message will be given. In my case, I’m selecting cURL and then click on Choose.

In the Integrate section, in steps 1 and 2, the wizard will help us out to download the language that we chose in the previous step. We need to create an API key, type a name, and click on Create API Key, and that will generate a random key that will be assigned to the API Name that we have just created. We will use that API Key to authenticate our code and allow it to send messages using the Web API interface.

In steps 3 and 4 of the same page, we have in step 3 a command to export the API Key to a variable, and in step 4 we have the actual cURL command that will be used to send the original message.

The code for cURL generated by the tool is listed below, and I highlighted the parameters/variables that you can change, which is the to, from, subject and body in the code.

curl --request POST \
--url https://api.sendgrid.com/v3/mail/send \
--header "Authorization: Bearer $SENDGRID_API_KEY" \
--header ‘Content-Type: application/json’ \
--data ‘{"personalizations": [{"to": [{"email": "recipient@yourdomain.ca"}]}],"from": {"email": "sender@yourdomain.ca"},"subject": "Hello World with SendGrid","content": [{"type": "text/plain", "value": "and easy to do anywhere, even with cURL"}]}’

The final step is to put the code to the test. Just type in both commands provided by the GridSend wizard on your command prompt and the result should be similar to the image seen below.

If after testing the command no error is presented, that is a good sign. However, the feature provides a better way to prove it. Go back to the wizard and click on the checkbox I’ve executed the code and click on Next: Verify Integration button.

In the Verify page, a button Verify Integration will be available in the middle of the screen. Click on it. The following page will give you a result of the process, as shown in the image below. You may be wondering how they tested our script. Well, all outbound email will be through their services, so it is easier for them to validate if any message went through. Click on View Email Activity.

The wizard will go to the Activity area, which is always accessible from the left menu. A list of all messages processed by the system will be listed.

The result will be a new message in your Inbox that you configured as a recipient of the test message.

Just a few clicks and you’re on your way

In just a few clicks, we enabled our organization to send outbound messages without configuring a single relay server, without creating allow lists, or permissions on your receive connectors. Using this feature does not require a lot of changes in your existent applications and savea time from the local team to troubleshoot mail-flow issues that may arise from having a local server. Keep in mind that we only went over the basics of the service. It is a best practice to update your SPF and DKIM settings to reflect SendGrid infrastructure before going live with this solution.

Featured image: Shutterstock

Anderson Patricio

Anderson Patricio is a Canadian MVP in Cloud and Datacenter Management, and Office Server and Services, besides of the Microsoft Award he also holds a Solutions Master (MCSM) in Exchange, CISSP and several other certifications. Anderson contributes to the Microsoft Community with articles, tutorials, blog posts, twitter, forums and book reviews. He is a regular contributor here at Techgenix.com, MSExchange.org, ITPROCentral.com and Anderson Patricio.org (Portuguese).

Share
Published by
Anderson Patricio

Recent Posts

No excuses: Patch Exchange and patch your operating systems

Admins hate patches. Admins hate patching. But with the threats lurking on the Internet and elsewhere, it’s time to accept…

3 hours ago

What does the future hold for the hosting industry?

As with other technology industries, the hosting business has seen major changes since the early days of the Internet. But…

6 hours ago

Moving a VM to a different virtual network in Microsoft Azure

Thinking of moving a VM to a different virtual network in Azure? It’s possible. Here’s how to avoid speed bumps…

23 hours ago

Safeguarding your digital identities in a hostile world

In today’s online world where everything is tracked and saved, safeguarding digital identities is crucial both for individuals and for…

1 day ago

Exchange errors: Common problems and commonsense fixes

Exchange errors are the curse of every IT admin’s job. Here are some common issues you may face — and…

1 day ago

Losing your edge? 7 free tools to keep you focused at work

Staying focused at work in an always-connected world is hard! Here’s how to use tech — and some free tools…

2 days ago