---
"@context": https://schema.org
"@type": TechArticle
"@id": https://www.twilio.com/docs/sendgrid/for-developers/tracking-events/csharp-code-example#article
headline: Event Webhook C# Code Example
description: Implement email parsing from the SendGrid domain and post to a specific URL using our proven methods in ApiController.
url: https://www.twilio.com/docs/sendgrid/for-developers/tracking-events/csharp-code-example
inLanguage: en
dateModified: 2026-07-23T16:57:51.000Z
author:
  "@type": Organization
  name: Twilio Developer Education Team
publisher:
  "@type": Organization
  name: Twilio
---

# Event Webhook C# Code Example

## Parse Webhook

This C# example parses all emails sent to `address@email.sendgrid.biz` and posts the parsed email to `http://sendgrid.biz/upload`.

Set the following parameters on the [Parse API settings page](https://sendgrid.com/developer/reply):

```bash {title="Inbound Parse hostname"}
Hostname: email.sendgrid.biz
```

```bash {title="Inbound Parse POST URL"}
URL: http://sendgrid.biz/upload
```

Put this C# model in your models folder:

```csharp {title="Email model for the Inbound Parse POST"}
/// <summary>
/// A model with the data format of the Inbound Parse API's POST
/// </summary>
public class Email
{
  /// <summary>
  /// The Domain Keys Identified Email code for the email
  /// </summary>
  public string Dkim { get; set; }

  /// <summary>
  /// The email address that the email was sent to
  /// </summary>
  public string To { get; set; }

  /// <summary>
  /// The HTML body of the email
  /// </summary>
  public string Html { get; set; }

  /// <summary>
  /// The email address the email was sent from
  /// </summary>
  public string From { get; set; }

  /// <summary>
  /// The Text body of the email
  /// </summary>
  public string Text { get; set; }

  /// <summary>
  /// The Ip address of the sender of the email
  /// </summary>
  public string SenderIp { get; set; }

  /// <summary>
  /// 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.
  /// </summary>
  public string Envelope { get; set; }

  /// <summary>
  /// Number of attachments included in email
  /// </summary>
  public int Attachments { get; set; }

  /// <summary>
  /// The subject of the email
  /// </summary>
  public string Subject { get; set; }

  /// <summary>
  /// A JSON string containing the character sets of the fields extracted from the message.
  /// </summary>
  public string Charsets { get; set; }

  /// <summary>
  /// The results of the Sender Policy Framework verification of the message sender and receiving IP address.
  /// </summary>
  public string Spf { get; set; }
}
```

To test this, send an email to `example@example.com` and add the following method to your `ApiController`. Don't forget the attribute.

```csharp {title="ApiController method to receive the Inbound Parse POST"}
// POST api/inbound
[HttpPost]
public async Task<HttpResponseMessage> 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);
}
```

The preceding code uses the following `using` statements:

```csharp {title="Required using statements"}
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
```
