Wrapping mailmessage headers in .net for Sendgrid
- by mickyjtwin
Am using SendGrid for some email notifications, specifically utilising their SMTP API's. While they have PHP examples, c# is not so helpful.
Essentially, a json string is built that contains the to email addresses, and other custom filters etc, which is then added to a MailMessage header to send.
// json string example
{"to":["address1@a.com", address2@a.com", "", ""],"sub":{"<name>":["Name1", Name2"]}}
MailMessage m = new MailMessage("to@to.com", "to@to.com");
m.Headers.Add("X-SMTPAPI", jsonString);
The problem I am having is that for some MTA's, headers can only be 1000 characters long, and also quoted-principle encoding required only 76 character per line.
In their php example, they are splitting the string and adding a linebreak (\n) every 72 characters. While I am doing this in .NET, I am receiving a invalid character exception. After some digging, it seems that pre-.NET4.0 Beta2 MailMessage will not process CLLR characters. Sengrid support is not proving helpful, and looking at ways to make this work?