This module helps build SendGrid's SMTP API headers.
All updates to this module is documented in our CHANGELOG.
- .NET version 4.5.2
- The SendGrid service
Update your Environment (user space) with your SENDGRID_USERNAME and SENDGRID_PASSWORD.
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
var header = new Header();
var uniqueArgs = new Dictionary<string,string> {
{ "foo", "bar" },
{ "chunky", "bacon"}
};
header.AddUniqueArgs(uniqueArgs);
var xmstpapiJson = header.JsonString();
You can then use generated JSON in conjunction with your favorite SMTP library.
SmtpClient client = new SmtpClient();
client.Port = 587;
client.Host = "smtp.sendgrid.net";
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("[email protected]"));
mail.From = new MailAddress("[email protected]");
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> {"[email protected]", "[email protected]", "[email protected]"};
header.SetTo(recipients);
var subs = new List<String> {"A","B","C"};
header.AddSubstitution("%name%", subs);
var mail = new MailMessage
{
From = new MailAddress("[email protected]"),
Subject = "Welcome",
Body = "Hi there %name%"
};
// add the custom header that we built above
mail.Headers.Add("X-SMTPAPI", header.JsonString());
If you are interested in the future direction of this project, please take a look at our milestones. We would love to hear your feedback.
We encourage contribution to our projects, please see our CONTRIBUTING guide for details.
Quick links:
smtpapi-csharp is guided and supported by the SendGrid Developer Experience Team.
smtpapi-csharp is maintained and funded by SendGrid, Inc. The names and logos for smtpapi-csharp are trademarks of SendGrid, Inc.