Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Event Webhook C# Code Example



Parse Webhook

parse-webhook page anchor

In this example, we want to parse all emails at address@email.sendgrid.biz and post the parsed email to http://sendgrid.biz/upload

Given this scenario, the following are the parameters you would set at the Parse API settings page(link takes you to an external page):


_10
Hostname: email.sendgrid.biz


_10
URL: http://sendgrid.biz/upload

Put this C# model in your models folder:


_60
/// <summary>
_60
/// A model with the data format of the Inbound Parse API's POST
_60
/// </summary>
_60
public class Email
_60
{
_60
/// <summary>
_60
/// The Domain Keys Identified Email code for the email
_60
/// </summary>
_60
public string Dkim { get; set; }
_60
_60
/// <summary>
_60
/// The email address that the email was sent to
_60
/// </summary>
_60
public string To { get; set; }
_60
_60
/// <summary>
_60
/// The HTML body of the email
_60
/// </summary>
_60
public string Html { get; set; }
_60
_60
/// <summary>
_60
/// The email address the email was sent from
_60
/// </summary>
_60
public string From { get; set; }
_60
_60
/// <summary>
_60
/// The Text body of the email
_60
/// </summary>
_60
public string Text { get; set; }
_60
_60
/// <summary>
_60
/// The Ip address of the sender of the email
_60
/// </summary>
_60
public string SenderIp { get; set; }
_60
_60
/// <summary>
_60
/// A JSON string containing the SMTP envelope. This will have 2 variables: to, which is an array of recipients, and from, which is the return path for the message.
_60
/// </summary>
_60
public string Envelope { get; set; }
_60
_60
/// <summary>
_60
/// Number of attachments included in email
_60
/// </summary>
_60
public int Attachments { get; set; }
_60
_60
/// <summary>
_60
/// The subject of the email
_60
/// </summary>
_60
public string Subject { get; set; }
_60
_60
/// <summary>
_60
/// A JSON string containing the character sets of the fields extracted from the message.
_60
/// </summary>
_60
public string Charsets { get; set; }
_60
_60
/// <summary>
_60
/// The results of the Sender Policy Framework verification of the message sender and receiving IP address.
_60
/// </summary>
_60
public string Spf { get; set; }
_60
}

To test this, we send an email to example@example.com, and put the following method in our ApiController. Note: Don't forget the attribute.


_27
// POST api/inbound
_27
[HttpPost]
_27
public async Task<HttpResponseMessage> Post()
_27
{
_27
var root = HttpContext.Current.Server.MapPath("~/App_Data");
_27
var provider = new MultipartFormDataStreamProvider(root);
_27
await Request.Content.ReadAsMultipartAsync(provider);
_27
_27
var email = new Email
_27
{
_27
Dkim = provider.FormData.GetValues("dkim").FirstOrDefault(),
_27
To = provider.FormData.GetValues("to").FirstOrDefault(),
_27
Html = provider.FormData.GetValues("html").FirstOrDefault(),
_27
From = provider.FormData.GetValues("from").FirstOrDefault(),
_27
Text = provider.FormData.GetValues("text").FirstOrDefault(),
_27
SenderIp = provider.FormData.GetValues("sender_ip").FirstOrDefault(),
_27
Envelope = provider.FormData.GetValues("envelope").FirstOrDefault(),
_27
Attachments = int.Parse(provider.FormData.GetValues("attachments").FirstOrDefault()),
_27
Subject = provider.FormData.GetValues("subject").FirstOrDefault(),
_27
Charsets = provider.FormData.GetValues("charsets").FirstOrDefault(),
_27
Spf = provider.FormData.GetValues("spf").FirstOrDefault()
_27
};
_27
_27
// The email is now stored in the email variable
_27
_27
return new HttpResponseMessage(HttpStatusCode.OK);
_27
}

The above code used the following using's


_10
using System.Linq;
_10
using System.Net;
_10
using System.Net.Http;
_10
using System.Threading.Tasks;
_10
using System.Web;
_10
using System.Web.Http;


Rate this page: