SendGrid Initial Setup in Azure

Looking at a basic email setup for an Azure hosted app SendGrid offers a free low volume (25k emails a month) solution that is well rounded.

You can easily add a free SendGrid account by using the Azure marketplace to add it to your existing Azure account.

SendGridSetup

Then once added you can click into it to get your username and password using the “Connection Info” link at the bottom.

SendGridCreds

Once you’ve got this you can install the SendGrid libraries via Nuget Package manager


PM> Install-Package SendGrid

Below is an example of a method that sends an email with SendGrid, its pretty similar to your standard SMTP mail in the dotnet framework libraries


Public Sub SendAnEmail(mailId As Integer, FromAddress As String _
, ToAddress As String, CCAddress As String _
, BCCAddress As String, Subject As String, Body As String)
' Create the email object first, then add the properties.
Dim myMessage = New SendGridMessage()

' Add the message properties.
myMessage.From = New MailAddress(FromAddress)

' Add multiple addresses to the To field.
Dim recipients As New List(Of [String])() From { _
ToAddress
}

myMessage.AddTo(recipients)
If CCAddress.Length <> 0 Then
myMessage.AddCc(CCAddress)
End If
If BCCAddress.Length <> 0 Then
myMessage.AddBcc(BCCAddress)
End If

myMessage.Subject = Subject
myMessage.DisableClickTracking()
'Add the HTML and Text bodies
myMessage.Html = Body

' Create credentials, specifying your user name and password.
Dim SGUser As String = ConfigurationManager.AppSettings("SGUser")
Dim SGPass As String = ConfigurationManager.AppSettings("SGPass")
Dim credentials = New NetworkCredential(SGUser, SGPass)

' Create an Web transport for sending email.
Dim transportWeb = New SendGrid.Web(credentials)

' Send the email.
transportWeb.DeliverAsync(myMessage)

End Sub

There is a few other steps i usually do too, you will note in the above method i’ve set the click trough tracking to disabled. This is because i have had issues with it before and the links not working on some odd mail clients.

Also by default SendGrid will “process” your bounces, so you’ll need to login to their dashboard to find them, Most of my users don’t want another dashboard to login to so i normally setup an auto-forward. This can be setup in their interface as per below shot.

SendGridAutoForward

If you need to access the bounce history its under the “Suppression” section.

I also recommend setting up the white label

Sendgridwhitelabel

I’ll do another post about setting this up as its not easy, SPF and DKIM are essential to have setup, but can be a pain in the ass to get going, SendGrid does make the processes easier though

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s