Skip to contentSkip to navigationSkip to topbar
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):

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 POST
3
/// </summary>
4
public class Email
5
{
6
/// <summary>
7
/// The Domain Keys Identified Email code for the email
8
/// </summary>
9
public string Dkim { get; set; }
10
11
/// <summary>
12
/// The email address that the email was sent to
13
/// </summary>
14
public string To { get; set; }
15
16
/// <summary>
17
/// The HTML body of the email
18
/// </summary>
19
public string Html { get; set; }
20
21
/// <summary>
22
/// The email address the email was sent from
23
/// </summary>
24
public string From { get; set; }
25
26
/// <summary>
27
/// The Text body of the email
28
/// </summary>
29
public string Text { get; set; }
30
31
/// <summary>
32
/// The Ip address of the sender of the email
33
/// </summary>
34
public string SenderIp { get; set; }
35
36
/// <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>
39
public string Envelope { get; set; }
40
41
/// <summary>
42
/// Number of attachments included in email
43
/// </summary>
44
public int Attachments { get; set; }
45
46
/// <summary>
47
/// The subject of the email
48
/// </summary>
49
public string Subject { get; set; }
50
51
/// <summary>
52
/// A JSON string containing the character sets of the fields extracted from the message.
53
/// </summary>
54
public string Charsets { get; set; }
55
56
/// <summary>
57
/// The results of the Sender Policy Framework verification of the message sender and receiving IP address.
58
/// </summary>
59
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.

1
// POST api/inbound
2
[HttpPost]
3
public async Task<HttpResponseMessage> Post()
4
{
5
var root = HttpContext.Current.Server.MapPath("~/App_Data");
6
var provider = new MultipartFormDataStreamProvider(root);
7
await Request.Content.ReadAsMultipartAsync(provider);
8
9
var email = new Email
10
{
11
Dkim = provider.FormData.GetValues("dkim").FirstOrDefault(),
12
To = provider.FormData.GetValues("to").FirstOrDefault(),
13
Html = provider.FormData.GetValues("html").FirstOrDefault(),
14
From = provider.FormData.GetValues("from").FirstOrDefault(),
15
Text = provider.FormData.GetValues("text").FirstOrDefault(),
16
SenderIp = provider.FormData.GetValues("sender_ip").FirstOrDefault(),
17
Envelope = provider.FormData.GetValues("envelope").FirstOrDefault(),
18
Attachments = int.Parse(provider.FormData.GetValues("attachments").FirstOrDefault()),
19
Subject = provider.FormData.GetValues("subject").FirstOrDefault(),
20
Charsets = provider.FormData.GetValues("charsets").FirstOrDefault(),
21
Spf = provider.FormData.GetValues("spf").FirstOrDefault()
22
};
23
24
// The email is now stored in the email variable
25
26
return new HttpResponseMessage(HttpStatusCode.OK);
27
}

The above code used the following using's

1
using System.Linq;
2
using System.Net;
3
using System.Net.Http;
4
using System.Threading.Tasks;
5
using System.Web;
6
using System.Web.Http;

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.