Free trial *Internet Service Required

How to Send Email Using SendGrid with Windows Azure

This guide demonstrates how to perform common programming tasks with the SendGrid email service on Windows Azure. The samples are written in C# and use the .NET API. The scenarios covered include constructing email, sending email, adding attachments, and using filters. For more information on SendGrid and sending email, see the Next steps section.

 

Table of contents

What is the SendGrid Email Service?
Create a SendGrid account
Reference the SendGrid .NET class library
How to: Create an email
How to: Send an email
How to: Add an attachment
How to: Use filters to enable footers, tracking, and analytics
How to: Use additional SendGrid services
Next steps

What is the SendGrid Email Service?What is the SendGrid Email Service?

SendGrid is a cloud-based email service that provides reliable email delivery, scalability, and real-time analytics along with flexible APIs that make custom integration easy. Common SendGrid usage scenarios include:

  • Automatically sending receipts to customers.
  • Administering distribution lists for sending customers monthly e-fliers and special offers.
  • Collecting real-time metrics for things like blocked e-mail, and customer responsiveness.
  • Generating reports to help identify trends.
  • Forwarding customer inquiries.

For more information, see http://sendgrid.com.

Create a SendGrid accountCreate a SendGrid account

Windows Azure customers can unlock 25,000 free emails each month. These 25,000 free monthly emails will give you access to advanced reporting and analytics and all APIs (Web, SMTP, Event, Parse, Sub-User). For information about additional services provided by SendGrid, see the SendGrid Features page.

To sign up for a SendGrid account

  1. Log in to the Windows Azure Management Portal.

  2. In the lower pane of the management portal, click New.

    command-bar-new

  3. Click Store.

    sendgrid-store

  4. In the Choose an Add-on dialog, select SendGrid and click the right arrow.

  5. In the Personalize Add-on dialog select the SendGrid plan you want to sign up for.

  6. Enter a name to identify your SendGrid service in your Windows Azure settings, or use the default value of SendGrid. Names must be between 1 and 100 characters in length and contain only alphanumeric characters, dashes, dots, and underscores. The name must be unique in your list of subscribed Windows Azure Store Items.

    store-screen-2

  7. Choose a value for the region; for example, West US.

  8. Click the right arrow.

  9. On the Review Purchase tab, review the plan and pricing information, and review the legal terms. If you agree to the terms, click the check mark. After you click the check mark, your SendGrid account will begin the provisioning process.

    store-screen-3

  10. After confirming your purchase you are redirected to the add-ons dashboard and you will see the message Purchasing SendGrid.

    sendgrid-purchasing-message

Your SendGrid account is provisioned immediately and you will see the message Successfully purchased Add-On SendGrid. Your account and credentials are now created. You are ready to send emails at this point.

To modify your subscription plan or see the SendGrid contact settings, click the name of your SendGrid service to open the SendGrid add-ons dashboard.

sendgrid-add-on-dashboard

To send an email using SendGrid, you must supply your account credentials (username and password).

To find your SendGrid credentials

  1. Click Connection Info.

    sendgrid-connection-info-button

  2. In the Connection info dialog, copy the Password and Username to use later in this tutorial.

    sendgrid-connection-info

To set your email deliverability settings, click the Manage button. This will open the Sendgrid.com web interface where you can login and open your SendGrid Control Panel.

sendgrid-control-panel

For more information on getting started with SendGrid, see SendGrid Getting Started.

Reference the SendGrid .NET class libraryReference the SendGrid .NET class library

The SendGrid NuGet package is the easiest way to get the SendGrid API and to configure your application with all dependencies. NuGet is a Visual Studio extension included with Microsoft Visual Studio 2012 that makes it easy to install and update libraries and tools.

Note

To install NuGet if you are running a version of Visual Studio earlier than Visual Studio 2012, visit http://www.nuget.org, and click theInstall NuGet button.

To install the SendGrid NuGet package in your application, do the following:

  1. In Solution Explorer, right-click References, then click Manage NuGet Packages.

  2. In the left-hand pane of the Manage NuGet Packages dialog, click Online.

  3. Search for SendGrid and select the SendGrid item in the results list.

    SendGrid NuGet package

  4. Click Install to complete the installation, and then close this dialog.

SendGrid's .NET class library is called SendGridMail. It contains the following namespaces:

  • SendGridMail for creating and working with email items.
  • SendGridMail.Transport for sending email using either the SMTP protocol, or the HTTP 1.1 protocol with Web/REST.

Add the following code namespace declarations to the top of any C# file in which you want to programmatically access the SendGrid email service. System.Net and System.Net.Mail are .NET Framework namespaces that are included because they include types you will commonly use with the SendGrid APIs.

using System.Net;
using System.Net.Mail;
using SendGridMail;
using SendGridMail.Transport;

How to: Create an emailHow to: Create an email

Use the static SendGrid.GenerateInstance method to create an email message that is of type SendGrid. Once the message is created, you can use SendGrid properties and methods to set values including the email sender, the email recipient, and the subject and body of the email.

The following example demonstrates how to create a fully populated email object:

// Setup the email properties.
var from = new MailAddress("john@contoso.com");
var to   = new MailAddress[] { new MailAddress("jeff@contoso.com") };
var cc   = new MailAddress[] { new MailAddress("anna@contoso.com") };
var bcc  = new MailAddress[] { new MailAddress("peter@contoso.com") };
var subject = "Testing the SendGrid Library";
var html    = "<p>Hello World!</p>";
var text = "Hello World plain text!";
var transport = SendGridMail.TransportType.SMTP;

// Create an email, passing in the the eight properties as arguments.
SendGrid myMessage = SendGrid.GenerateInstance(from, to, cc, bcc, subject, html, text, transport);

The following example demonstrates how to create an empty email object:

// Create the email object first, then add the properties.
SendGrid myMessage = SendGrid.GenerateInstance();

// Add the message properties.
MailAddress sender = new MailAddress(@"John Smith <john@contoso.com>");
myMessage.From = sender;

// Add multiple addresses to the To field.
List<String> recipients = new List<String>
{
    @"Jeff Smith <jeff@contoso.com>",
    @"Anna Lidman <anna@contoso.com>",
    @"Peter Saddow <peter@contoso.com>"
};

foreach (string recipient in recipients)
{
    myMessage.AddTo(recipient);
}

// Add a message body in HTML format.
myMessage.Html = "<p>Hello World!</p>";

// Add the subject.
myMessage.Subject = "Testing the SendGrid Library";

For more information on all properties and methods supported by the SendGrid type, see sendgrid-csharp on GitHub.

How to: Send an emailHow to: Send an email

After creating an email message, you can send it using either SMTP or the Web API provided by SendGrid. For details about the benefits and drawbacks of each API, see SMTP vs. Web API in the SendGrid documentation.

Sending email with either protocol requires that you supply your SendGrid account credentials (username and password). The following code demonstrates how to wrap your credentials in a NetworkCredential object:

// Create network credentials to access your SendGrid account.
var username = "your_sendgrid_username";
var pswd = "your_sendgrid_password";

var credentials = new NetworkCredential(username, pswd);

To send an email message, use the Deliver method on either the SMTP class, which uses the SMTP protocol, or the REST transport class, which calls the SendGrid Web API. The following examples show how to send a message using both SMTP and the Web API.

SMTP

// Create the email object first, then add the properties.
SendGrid myMessage = SendGrid.GenerateInstance();
myMessage.AddTo("anna@contoso.com");
myMessage.From = new MailAddress("john@contoso.com", "John Smith");
myMessage.Subject = "Testing the SendGrid Library";
myMessage.Text = "Hello World!";

// Create credentials, specifying your user name and password.
var credentials = new NetworkCredential("username", "password");

// Create an SMTP transport for sending email.
var transportSMTP = SMTP.GenerateInstance(credentials);

// Send the email.
transportSMTP.Deliver(myMessage);

Web API

// Create the email object first, then add the properties.
SendGrid myMessage = SendGrid.GenerateInstance();
myMessage.AddTo("anna@contoso.com");
myMessage.From = new MailAddress("john@contoso.com", "John Smith");
myMessage.Subject = "Testing the SendGrid Library";
myMessage.Text = "Hello World!";

// Create credentials, specifying your user name and password.
var credentials = new NetworkCredential("username", "password");

// Create an REST transport for sending email.
var transportREST = REST.GetInstance(credentials);

// Send the email.
transportREST.Deliver(myMessage);

How to: Add an attachmentHow to: Add an attachment

Attachments can be added to a message by calling the AddAttachment method and specifying the name and path of the file you want to attach. You can include multiple attachments by calling this method once for each file you wish to attach. The following example demonstrates adding an attachment to a message:

SendGrid myMessage = SendGrid.GenerateInstance();
myMessage.AddTo("anna@contoso.com");
myMessage.From = new MailAddress("john@contoso.com", "John Smith");
myMessage.Subject = "Testing the SendGrid Library";
myMessage.Text = "Hello World!";

myMessage.AddAttachment(@"C:\file1.txt");

How to: Use filters to enable footers, tracking, and analyticsHow to: Use filters to enable footers, tracking, and analytics

SendGrid provides additional email functionality through the use of filters. These are settings that can be added to an email message to enable specific functionality such as click tracking, Google analytics, subscription tracking, and so on. For a full list of filters, see Filter Settings.

Filters can be applied to SendGrid email messages using methods implemented as part of the SendGrid class. Before you can enable filters on an email message, you must first initialize the list of available filters by calling the InitalizeFilters method.

The following examples demonstrate the footer and click tracking filters:

Footer

// Create the email object first, then add the properties.
SendGrid myMessage = SendGrid.GenerateInstance();
myMessage.AddTo("anna@contoso.com");
myMessage.From = new MailAddress("john@contoso.com", "John Smith");
myMessage.Subject = "Testing the SendGrid Library";
myMessage.Text = "Hello World!";

myMessage.InitializeFilters();
// Add a footer to the message.
myMessage.EnableFooter("PLAIN TEXT FOOTER", "<p><em>HTML FOOTER</em></p>");

Click tracking

// Create the email object first, then add the properties.
SendGrid myMessage = SendGrid.GenerateInstance();
myMessage.AddTo("anna@contoso.com");
myMessage.From = new MailAddress("john@contoso.com", "John Smith");
myMessage.Subject = "Testing the SendGrid Library";
myMessage.Html = "<p><a href=\"http://www.windowsazure.com\">Hello World Link!</a></p>";
myMessage.Text = "Hello World!";

myMessage.InitializeFilters();
// true indicates that links in plain text portions of the email 
// should also be overwritten for link tracking purposes. 
myMessage.EnableClickTracking(true);

How to: Use additional SendGrid servicesHow to: Use additional SendGrid services

SendGrid offers web-based APIs that you can use to leverage additional SendGrid functionality from your Windows Azure application. For full details, see the SendGrid API documentation.

Next stepsNext steps

Now that you’ve learned the basics of the SendGrid Email service, follow these links to learn more.

Rss Newsletter