I recently launched a daily email newsletter on my company’s investing website, American Banking & Market News. The newsletter provides daily updates about ratings changes made by stock analysts. Initially, I planned on using my server’s local SMTP server to send out the messages, but realized that after getting more than 50 sign-ups the first day, the number of subscribers could quickly overwhelm my ability to stay off blacklists and keep email delivery rates high.
I looked at a number of services, including those that provide full management of a distribution list and email delivery, such as Mail Chimp and Aweber. I also looked at services which only provide white-listing such as SendGrid and JangoSMTP. MailChimp would bet overkill for what I was hoping to do and didn’t provide much for ASP.NET integration. Aweber has no API or SMTP service to speak of. I opted to go with SendGrid because the price was reasonable and they do a good job of ensuring delivery.
There are two ways to send out email via SendGrid in ASP.NET. You can use the ASP.NET WebClient to create a simple web-request that will send out an email. The specifications for the request you should make to do this are available on SendGrid’s website. You can also use the built in System.Net.Mail library to send out an email using SendGrid. I opted to use the System.Net.Mail method because using the WebClient method resulted in URI length errors for email messages of any significant size.
Here’s the code that I’m using:
var smtp = new SmtpClient
Host = “smtp.sendgrid.net”,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Port = 25,
Credentials = new NetworkCredential(“firstname.lastname@example.org”, “password”)
MailMessage thisMessage = new MailMessage(fromAddress, toAddress);
thisMessage.IsBodyHtml = true;
thisMessage.Subject = “Title Here”;
thisMessage.Body = “Sample Body”;
foreach (DataRow thisEmail in dtEmails.Rows)
if (thisMessage.Bcc.Count > 500)
You’ll notice that the code above is very much in-line with what it takes to send an email via any other SMTP provider. There is some additional code (which will not work on your website without modification) which provides the ability to add a large number of recipients via BCC. I have a DataTable called “dtEmails” that contains a list of email addresses (in the “AccountEmail” field). The system will loop through them and add them to the message.
You’ll notice a few lines of code which checks and sees if there are more than 500 BCC recipients. If the conditional statement finds there are that many recipients, it will send the email and clear out the BCC recipients that have already gotten email, effectively splitting the email into blocks of 500 recipients at a time. I added this code base off a recommendation from SendGrid’s SMTP API page, where they write, “While there is no hard limit to the number of addresses that can be sent to in a multiple recipient e-mail, it is best to split up large jobs at around 1,000 recipients, as this will better allow for the processing load to be distributed.”