How to receive incoming emails in your MVC app

now sends an email asking its users how their day went. They can reply to the email directly and their answer is directly added to their journal.

This is how I have implemented this feature using SendGrid and MVC3.

1. Set up your DNS

SendGrid needs you to set up a dedicated domain name that points to their email servers. For example, for WEEK PLAN, I use and the emails I send the reply-able emails from uses this convention:

If a user replies to one of these emails, SendGrid would be processing it.

MX record

2. Set up incoming emails in SendGrid

Each time SendGrid receives an email, they will parse it and post it to a URL you give them (POST request). Simply go to the Developers section, and click on the “Parse Incoming Emails” link:

SendGrid settings page

3. Set up a new route in MVC

WEEK PLAN Now we need to make that URL work. I simply need to add the following in my Global.asax.cs:

Adding a new route

4. Code the action

This is the code of the receiving action:

A few comments on this code:

1. The ValidateInput(false) attribute prevents MVC to consider the incoming data as not safe.

2. I have created a class IncomingEmail to take advantage of the default binder in MVC. I simply name the properties the same way SendGrid names the different parameters.

3. I wasn’t sure what type should the envelope parameter be so I decided to use a dynamic object  for that (and I needed to deserialize the envelope parameter into the dynamic object). Probably not the best option, I am happy to receive suggestions on what type should envelope be.

That’s it, the rest of the action is simply parsing the email address ( for example to know what to do.

I Hope WEEK PLAN users will find this useful.