Tracking Email Using Microsoft Azure and the SendGrid Event Webhook (Part 1)


July 21, 2014
Written by
Elmer Thomas
Contributor
Opinions expressed by Twilio contributors are their own

At the end of this three-part blog post, you will have created a Windows Phone app that can receive push notifications when certain email events happen (e.g. email opened or link clicked), in near real-time.

In this first post, we'll implement the SendGrid Event Webhook listener. Specifically, a C# ASP.NET application on Azure.

Part two will cover creating a Windows Phone 8.1 app that displays email tracking data via the SendGrid Web API.

In part three, we'll send push notifications from our SendGrid Event Webhook listener when certain events, such as a bounce, occur.

Prerequisites/Assumptions

This code (source) was developed within VMWare Fusion 6 on Mac OS X Mavericks, running Windows 8.1 in Visual Studio Express 2013 for Web. You may find the source code for this tutorial on Github. This code was also tested on a Microsoft Surface Pro running Windows 8.1.

Setting up SendGrid and the Event Webhook

First, you'll need a SendGrid account (a free account is sufficient). Next, you need to configure the Event Webhook as described under the heading Setup here.

Creating a Microsoft Azure Website

This tutorial was built on top of the ASP.NET Web Application template for C#, which is included with Visual Studio Express 2013 for Web. If you are new to ASP.NET and Microsoft Azure development, I recommend this tutorial. After completing that tutorial you will have a working web application that you can easily modify with the code provided.

Building the Model

We need to create a class that stores the data we receive from the Event Webhook. You can find that class here. Below is a snippet of that class:

Building the Controller

We need to define the endpoint (api/SendGrid in this case) and deserialize the POSTed JSON. You can find that code here. Below is a snippet:

Demo

Following is short video that demonstrates the final working code:



Remember to check back for parts two and three in this series.

Do you have any ideas for how you can incorporate this data into your applications? I hope so :)

Happy Hacking!

Send With Confidence

Partner with the email service trusted by developers and marketers for time-savings, scalability, and delivery expertise.

using Newtonsoft.Json;
using System.Collections.Generic;
namespace SendGridEventWebhook.Models
{
// This class models the data we POST from our Event Webhook stream
public class SendGridEvents
{
// Docs: https://sendgrid.com/docs/API_Reference/Webhooks/event.html
public string email { get; set; }
public int timestamp { get; set; }
public int uid { get; set; }
public int id { get; set; }
public string sendgrid_event_id { get; set; }
[JsonProperty("smtp-id")] // switched to underscore for consistancy
public string smtp_id { get; set; }
public string sg_message_id { get; set; }
[JsonProperty("event")] // event is protected keyword
public string sendgrid_event { get; set; }
public string type { get; set; }
public IList<string> category { get; set; }
public string reason { get; set; }
public string status { get; set; }
public string url { get; set; }
public string useragent { get; set; }
public string ip { get; set; }
// Add your custom fields here
public string purchase { get; set; } // this is a custom field sent by our tester
}
}
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using SendGridEventWebhook.Models;
using Newtonsoft.Json;
namespace SendGridEventWebhook.Controllers
{
// Display the default home page for this example
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
// Capture the SendGrid Event Webhook POST's at the /api/SendGrid endpoint
public class apiController : Controller
{
[HttpPost]
[ValidateInput(false)]
public ActionResult SendGrid()
{
System.IO.StreamReader reader = new System.IO.StreamReader(HttpContext.Request.InputStream);
string rawSendGridJSON = reader.ReadToEnd();
List<SendGridEvents> sendGridEvents = JsonConvert.DeserializeObject<List<SendGridEvents>>(rawSendGridJSON);
string count = sendGridEvents.Count.ToString();
System.Diagnostics.Trace.TraceError(rawSendGridJSON); // For debugging to the Azure Streaming logs
foreach (SendGridEvents sendGridEvent in sendGridEvents)
{
// Here is where you capture the event data
System.Diagnostics.Trace.TraceError(sendGridEvent.email); // For debugging to the Azure Streaming logs
}
return new HttpStatusCodeResult(200);
}
}
}