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
- Part 1 - Creating and Deploying an ASP.NET MVC Web App to Windows Azure
- Part 2 - Connecting an ASP.NET MVC Web App with SQL Azure
- Part 3 - Connecting SQL Management Studio to SQL Azure
- Part 4a - How To…Verify Email Address: Entity Framework Code First Migrations
- Part 4b - How To…Verify Email Address: Setting Up SendGrid 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:
- Some form of on-premise email forwarding solution.
- Use a general purpose third party SMTP service.
- Run an SMTP server of your own inside Azure.
- Send your emails via an Office365 account.
- 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:
- Go to the Azure Management Portal
- Click the ‘New’ button in the bottom left corner
- Choose the ‘Store’ options:
- This should open a pop-up where you need to find the SendGrid option. Select it, and press the next button:
- 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):
- Finally select the purchase option (note that I chose the free plan and as such have no costs associated at this stage):
- 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
- Click on the provisioned SendGrid service within the azure management portal
- Next on the footer click ‘Connection Info’
- 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
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:
<network host="server" password="password" userName="username" port="587" />
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:
// Create a mail message
var message = new MailMessage();
// Add recipient
message.To.Add(new MailAddress("firstname.lastname@example.org", "Matt Whetton"));
// Set subject
message.Subject = "Welcome to Codenutz";
// Set HTML body
message.Body = "<p>Hello and welcome</p>";
message.IsBodyHtml = true;
// Create the smtp client
var smtpClient = new SmtpClient();
// Send the message
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.
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.