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:
_10Hostname: email.sendgrid.biz
_10URL: 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>_60public 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]_27public 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
_10using System.Linq;_10using System.Net;_10using System.Net.Http;_10using System.Threading.Tasks;_10using System.Web;_10using System.Web.Http;