Skip to content

Creating a Postmark Inbound Web Hook Receiver

jptoto edited this page Feb 1, 2012 · 2 revisions

Introduction

Recently Postmark deployed a new feature to allow a user to send or forward an email to a special inbound email address and automatically receive a "web hook" API call to a url resource of your choosing. The benefit of this service is that it takes away the complicated task of setting up an email server and parsing messages yourself. In order to use the service all you need to do is setup a server application to receive the web hook API call. This post and sample code will show you how to do that using the .NET WCF WebAPI component.

Creating the Project

For this example I've borrowed from the WCF WebAPI code hosted at CodePlex so you can refer to the tutorial for further API examples using WCF WebAPI. Just like in the CodePlex example, I've written this API receiver inside of an ASP.NET MVC3 web application. There are other ways to host a WCF WebAPI module but I think this example makes sense for a lot of users who might be integrating Postmark Inbound into their existing web applications.

  1. Begin by downloading the postmark-inbound-demo project. The project is a Visual Studio 2010 solution and assumes you have ASP.NET MVC3 installed. The test project runs Xunit so in order to run the tests you will want to install the Xunit tools.
  2. Open the solution. You should have two projects, postmark-inbound-demo and postmark-inbound-demo.tests.
  3. In the references section, you'll see we've added several new namespaces like System.ServiceModel by way of installing "WebAPI" from Nuget.
  4. Open the API folder under the web project. There is a class called PostmarkApi.cs. This is where our WebMethods are listed. These are the "end points" of the API. The [ServiceContract] attribute on the class indicates that this class should be exposed over http. There is one method that responds to an http post and returns an "Email" object.
  5. Open the Resources folder and take a look at the Email.cs class. This is modeled after the JSON data that is sent from the Postmark Inbound web hook. Apart from that the Email class is just a plain old C# object (POCO).
Clone this wiki locally