This module helps build SendGrid's SMTP API headers.
All updates to this module are documented in our CHANGELOG.
- .NET Framework 4.0+
- The SendGrid service
Update the development environment with your SENDGRID_API_KEY, for example:
- Make a copy of the .env_sample file and name it .env
- Edit the file changing only the value of SENDGRID_PASSWORD variable
- Save the file
- Add the .env file to your environment path
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.SmtpApiOnce 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
varheader=newHeader();varuniqueArgs=newDictionary<string,string>{{"foo","bar"},{"chunky","bacon"}};header.AddUniqueArgs(uniqueArgs);varxmstpapiJson=header.JsonString();You can then use the generated JSON in conjunction with your favorite SMTP library.
SmtpClientclient=newSmtpClient();client.Port=587;client.Host="smtp.sendgrid.net";client.Timeout=10000;client.DeliveryMethod=SmtpDeliveryMethod.Network;client.UseDefaultCredentials=false;client.Credentials=newSystem.Net.NetworkCredential("your_sendgrid_username","your_sendgrid_password");MailMessagemail=newMailMessage();mail.To.Add(newMailAddress("user@example.com"));mail.From=newMailAddress("you@yourcompany.com");mail.Subject="this is a test email.";mail.Body="this is my test email body";// add the custom header that we built abovemail.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:
varheader=newHeader();varrecipients=newList<String>{"test1@example.com","test2@example.com","test3@example.com"};header.SetTo(recipients);varsubs=newList<String>{"A","B","C"};header.AddSubstitution("%name%",subs);varmail=newMailMessage{From=newMailAddress("test@example.com"),Subject="Welcome",Body="Hi there %name%"};// add the custom header that we built abovemail.Headers.Add("X-SMTPAPI",header.JsonString());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 using SendGrid, please check the Twilio SendGrid Support Help Center.
