Skip to content
Switch branches/tags
Go to file

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

SendGrid Logo

Travis Badge Email Notifications Badge Twitter Follow GitHub contributors MIT licensed

This module helps build SendGrid's SMTP API headers.


The default branch name for this repository has been changed to main as of 07/27/2020.

All updates to this module are documented in our CHANGELOG.

Table of Contents



Setup Environment Variables

Environment Variable

Update the development environment with your SENDGRID_API_KEY, for example:

  1. Make a copy of the .env_sample file and name it .env
  2. Edit the file changing only the value of SENDGRID_PASSWORD variable
  3. Save the file
  4. Add the .env file to your environment path

Install Package

To use SendGrid.SmtpApi in your C# project, you can either download the SendGrid C# .NET libraries directly from our Github repository or, if you have the NuGet package manager installed, you can grab them automatically.

PM> Install-Package SendGrid.SmtpApi

Once you have the library properly referenced in your project, you can include calls to them in your code.

For a sample implementation, check the Example

Quick Start

var header = new Header();

var uniqueArgs = new Dictionary<string,string> {
  { "foo", "bar" },
  { "chunky", "bacon"}


var xmstpapiJson = header.JsonString();

You can then use the generated JSON in conjunction with your favorite SMTP library.

SmtpClient client = new SmtpClient();
client.Port = 587;
client.Host = "";
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("your_sendgrid_username","your_sendgrid_password");

MailMessage mail = new MailMessage();
mail.To.Add(new MailAddress(""));
mail.From = new MailAddress("");
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";

// add the custom header that we built above
mail.Headers.Add( "X-SMTPAPI", xmstpapiJson );

client.SendAsync(mail, null);

If you want to add multiple recipients to the X-SMTPAPI header for a mail merge type send, you can do something like the following:

var header = new Header();

var recipients = new List<String> {"", "", ""};

var subs = new List<String> {"A","B","C"};
header.AddSubstitution("%name%", subs);

var mail = new MailMessage
    From = new MailAddress(""),
    Subject = "Welcome",
    Body = "Hi there %name%"

// add the custom header that we built above
mail.Headers.Add("X-SMTPAPI", header.JsonString());


How to Contribute

We encourage contribution to our projects, please see our CONTRIBUTING guide for details.

Quick links:


smtpapi-csharp is maintained and funded by Twilio SendGrid, Inc. The names and logos for smtpapi-csharp are trademarks of Twilio SendGrid, Inc.

If you need help installing or using the library, please check the Twilio SendGrid Support Help Center.

If you've instead found a bug in the library or would like new features added, go ahead and open issues or pull requests against this repo!


The MIT License (MIT)