A while back, SendGrid announced its newsletter functionality that provides functionality for managing and emailing distribution lists. If you’re a developer that makes use of SendGrid, you’re probably managing your list internally in a SQL database and have no need for SendGrid’s newsletter functionality, but there are two distinct advantages to using the Newsletter API.The first major advantage is that you can email a distribution list of any size with a single API call, which is much more efficient than trying to email a large distribution list one-by-one or in groups via SMTP.
The second major advantage is cost. Sending email through the newsletter API is about 45% cheaper than sending mail through SMTP or SendGrid’s Web API ($0.25 per 1,000 emails vs. $0.45 per 1,000 emails. This might not matter if you’re using a small volume, but it can make a difference on your bill if you’re sending out 1,000,000 or more emails each month. I saved about $250.00 per month from switching the majority of the messages sent by my server to using the Newsletter API, since my primary business is sending out a daily newsletter.
SendGrid does have a C# library that does a great job of sending messages through the Web API, but it does not currently support the newsletter API. Since I implemented the Newsletter API in my C# code-base, I thought I’d pass along a class I wrote called “SendGridTools” I wrote in the event that someone else wants to make use of the SendGrid Newsletter API in ASP.NET.
I’ve included most all of the common functions that should be needed:
- List Management Functions
- CreateDistributionList (Creates a new email distribution list)
- DeleteDistributionList (Deletes a email distribution list)
- Recipient Management Functions
- AddEmailToList (Adds an email to a distribution list)
- AddMultipleEmailstoList (Adds lots of emails to a distribution list)
- DeleteEmailFromList (Remove a user from a distribution list)
- Delivery Functions
- SendNewsletterToList (Send a message to a distribution list)
To make use of this class, simply import the C# file into the AppCode folder of your website or web-application and specify your SendGrid account credentials in the header of the class. The methods are based out of a simple static class titled “SendGridTools”.
Here’s some example code that makes use of the class. The code below creates a distribution list called “mylist”, adds three emails to that list and sends a message to that list.
SendGridTools.AddEmailToList("[email protected]", "example 1", "mylist");
SendGridTools.AddEmailToList("[email protected]", "example 2", "mylist");
SendGridTools.AddEmailToList("[email protected]", "example 3", "mylist");
SendGridTools.SendNewsletterToList("from name", "my newsletter", "this is my newsletter body", "mylist");
You are free to use the provided library for any commercial or non-commercial purpose. I release it to the public domain and do not reserve any copyright to it. Have questions about this library or suggestions to improve it? Feel free to make suggestions in the comments below.