| layout | page | ||
|---|---|---|---|
| weight | 10 | ||
| title | C# | ||
| navigation |
|
{% anchor h2 %} Parse Webhook {% endanchor %}
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:
{% codeblock %} Hostname: email.sendgrid.biz {% endcodeblock %}
{% codeblock %} URL: http://sendgrid.biz/upload {% endcodeblock %}
Put this C# model in your models folder:
{% codeblock lang:csharp %} ///
///
///
///
///
///
///
///
///
///
///
To test this, we send an email to [email protected], and put the following method in our ApiController. Note: Don't forget the attribute.
{% codeblock lang:csharp %} // POST api/inbound [HttpPost] public async Task Post() { var root = HttpContext.Current.Server.MapPath("~/App_Data"); var provider = new MultipartFormDataStreamProvider(root); await Request.Content.ReadAsMultipartAsync(provider);
var email = new Email
{
Dkim = provider.FormData.GetValues("dkim").FirstOrDefault(),
To = provider.FormData.GetValues("to").FirstOrDefault(),
Html = provider.FormData.GetValues("html").FirstOrDefault(),
From = provider.FormData.GetValues("from").FirstOrDefault(),
Text = provider.FormData.GetValues("text").FirstOrDefault(),
SenderIp = provider.FormData.GetValues("sender_ip").FirstOrDefault(),
Envelope = provider.FormData.GetValues("envelope").FirstOrDefault(),
Attachments = int.Parse(provider.FormData.GetValues("attachments").FirstOrDefault()),
Subject = provider.FormData.GetValues("subject").FirstOrDefault(),
Charsets = provider.FormData.GetValues("charsets").FirstOrDefault(),
Spf = provider.FormData.GetValues("spf").FirstOrDefault()
};
// The email is now stored in the email variable
return new HttpResponseMessage(HttpStatusCode.OK);
} {% endcodeblock %}
The above code used the following using's
{% codeblock lang:csharp %} using System.Linq; using System.Net; using System.Net.Http; using System.Threading.Tasks; using System.Web; using System.Web.Http; {% endcodeblock %}