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:
Hostname: email.sendgrid.biz
URL: http://sendgrid.biz/upload
Put this C# model in your models folder:
1/// <summary>2/// A model with the data format of the Inbound Parse API's POST3/// </summary>4public class Email5{6/// <summary>7/// The Domain Keys Identified Email code for the email8/// </summary>9public string Dkim { get; set; }1011/// <summary>12/// The email address that the email was sent to13/// </summary>14public string To { get; set; }1516/// <summary>17/// The HTML body of the email18/// </summary>19public string Html { get; set; }2021/// <summary>22/// The email address the email was sent from23/// </summary>24public string From { get; set; }2526/// <summary>27/// The Text body of the email28/// </summary>29public string Text { get; set; }3031/// <summary>32/// The Ip address of the sender of the email33/// </summary>34public string SenderIp { get; set; }3536/// <summary>37/// 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.38/// </summary>39public string Envelope { get; set; }4041/// <summary>42/// Number of attachments included in email43/// </summary>44public int Attachments { get; set; }4546/// <summary>47/// The subject of the email48/// </summary>49public string Subject { get; set; }5051/// <summary>52/// A JSON string containing the character sets of the fields extracted from the message.53/// </summary>54public string Charsets { get; set; }5556/// <summary>57/// The results of the Sender Policy Framework verification of the message sender and receiving IP address.58/// </summary>59public 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.
1// POST api/inbound2[HttpPost]3public async Task<HttpResponseMessage> Post()4{5var root = HttpContext.Current.Server.MapPath("~/App_Data");6var provider = new MultipartFormDataStreamProvider(root);7await Request.Content.ReadAsMultipartAsync(provider);89var email = new Email10{11Dkim = provider.FormData.GetValues("dkim").FirstOrDefault(),12To = provider.FormData.GetValues("to").FirstOrDefault(),13Html = provider.FormData.GetValues("html").FirstOrDefault(),14From = provider.FormData.GetValues("from").FirstOrDefault(),15Text = provider.FormData.GetValues("text").FirstOrDefault(),16SenderIp = provider.FormData.GetValues("sender_ip").FirstOrDefault(),17Envelope = provider.FormData.GetValues("envelope").FirstOrDefault(),18Attachments = int.Parse(provider.FormData.GetValues("attachments").FirstOrDefault()),19Subject = provider.FormData.GetValues("subject").FirstOrDefault(),20Charsets = provider.FormData.GetValues("charsets").FirstOrDefault(),21Spf = provider.FormData.GetValues("spf").FirstOrDefault()22};2324// The email is now stored in the email variable2526return new HttpResponseMessage(HttpStatusCode.OK);27}
The above code used the following using
's
1using System.Linq;2using System.Net;3using System.Net.Http;4using System.Threading.Tasks;5using System.Web;6using System.Web.Http;