How To…Verify Email Address: Setting Up SendGrid on Windows Azure

7 Flares Twitter 2 Facebook 0 StumbleUpon 1 Google+ 3 LinkedIn 1 7 Flares ×

sendgrid

In my previous article about how to verify an email address in ASP.NET MVC on Windows Azure, I covered changing your model and database with entity framework code first migrations. This leaves several more steps to complete:

1. Amend the model to allow for storing of emails and verification information.
2. Apply these model changes to the database.
3. Create a SendGrid account on azure and configure it within your web app.
4. Collect the additional information on user sign-up
5. Amend the registration process to send relevant emails and prevent automatic sign-in
6. Change the login process to ensure the user is verified before logging in
7. Handle the verification link for the specified user

In this series of posts I’ll be covering each of these steps as part of my ASP.NET Web Apps on Azure series.

Series: ASP.NET Web Apps on Windows Azure

This article will be digging into what SendGrid is and how to get it set up on Windows Azure.

Sending Email from Windows Azure

One thing that often catches developers (including myself) out when starting to develop ina cloud infrastructure like Windows Azure, is the seeming lack of certain services…such as an SMTP server. I think this catches us out because we’re used to working on virtual machines with hosting companies, or on premise within a larger network. In both of these situations its common to have an SMTP server readily available.

Windows Azure is web sites and web roles aren’t general purpose virtual machines – they’re web servers designed to serve web site – as such they don’t have these often unnecessary features such as an smtp server.

This leaves you with a few options for sending email:

  1. Some form of on-premise email forwarding solution.
  2. Use a general purpose third party SMTP service.
  3. Run an SMTP server of your own inside Azure.
  4. Send your emails via an Office365 account.
  5. Use an azure integrated SMTP service (i.e. SendGrid).

For this article I’ve chosen to go the SendGrid route for the following reasons:

  • Generally speaking when making apps we’re probably not going to have an on-premise environment. We want everything in the cloud with as little maintenance expense as possible.
  • I don’t really like backing my highly available windows azure environment with a general purpose 3rd party SMTP service. In my experience the management of these services can become painful, and the availability of them is not of the same standard as azure. Furthermore emails sent from these general purpose smtp servers get marked as spam way too often.
  • I’d rarely advise anybody trying to set-up something like an smtp server within azure – its complicated and fraught with issues. I’m sure some have successfully achieved this, but my worry is that you’d spend so much time setting it up and maintaining it that it may take over things.
  • Sending via Office365 is a great option, but as for option one, you may well not be using office 365.
  • SendGrid is really easy to set-up, its manageable from the azure management portal and offers a lot before you have to start paying for things.

In future articles I may cover some of these other options, such as using Office365 or a 3rd party provider, but right now we’re going to concentrate on SendGrid.

What is SendGrid

From the SendGrid documentation:

SendGrid is a cloud-based SMTP provider that allows you to send email without having to maintain email servers. SendGrid manages all of the technical details, from scaling the infrastructure to ISP outreach and reputation monitoring to whitelist services and real time analytics.

So SendGrid is all about making email easy to send from the cloud – in our case Windows Azure.

Getting Started With SendGrid

To get started you need to add SendGrid in the azure management portal:

  1. Go to the Azure Management Portal
  2. Click the ‘New’ button in the bottom left corner
    Azure Add New Button
  3. Choose the ‘Store’ options:
    Azure Add New Store Button
  4. This should open a pop-up where you need to find the SendGrid option. Select it, and press the next button:
    Azure Add Send Grid 1
  5. On this choose your pricing plane, what you want to call the sendgrid service within azure and where you want to locate it. Then click next. (I selected the free plan, called it MyFirstWebAppSendGrid and EastUS as the location):
    Azure Add Send Grid 2
  6. Finally select the purchase option (note that I chose the free plan and as such have no costs associated at this stage):
    Azure Add Send Grid 3
  7. Finally you should have to wait a few seconds, but you should see your SendGrid account is successfully configured within the windows azure management portal.

Using SendGrid Within Your Web App

Once all of this is configured, using SendGrid within you app is really straight forward.

Get SendGrid SMTP Connection Details

  1. Click on the provisioned SendGrid service within the azure management portal
    Select Send Grid Service
  2. Next on the footer click ‘Connection Info’
    SendGrid Select Connection Info
  3. You should next be presented with all the connection info for using the SendGrid SMTP service. Make a note of the username, password and server
    SendGrid Connection Info

Configuring Your Web App

Configuraing the SMTP server in your web app is really straight forward. All you need to do is open your web.config file and add the following section inside the configuration element:

Here you need to replace the server, username and password with the ones you got from the previous step. The from address should also be changed to something more relevant.

Sending an email

At this stage you have are ready to send emails from your web app using the standard .NET SMTP provider. Here is some sample code that demonstrates how you would send an email using the standard provider:

There are a lot of variations that can be added to this such as:

  • Specifying plain text version of email
  • Specifying from address
  • Adding attachments etc

But for now we have everything configured ready for sending our email verification messages.

Wrapping up

Next we’ll be looking at amending the user sign-up process to collect the additional data we require, send and handle the verification emails.

Please get in contact if you have anything you’d like to ask, any more detail, any feedback or just want to say that you found something useful. I’m always welcoming of feedback and love to hear that you’re finding these articles useful.

The Essential App Marketing Kit
Subscribe To My Newsletter To Get an Entire Chapter From The Book for FREE
Never display this again
7 Flares Twitter 2 Facebook 0 StumbleUpon 1 Google+ 3 LinkedIn 1 7 Flares ×