Wrapping mailmessage headers in .net for Sendgrid

Posted by mickyjtwin on Stack Overflow See other posts from Stack Overflow or by mickyjtwin
Published on 2010-05-26T01:47:50Z Indexed on 2010/05/26 1:51 UTC
Read the original article Hit count: 518

Filed under:
|
|
|

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":["[email protected]", [email protected]", "", ""],"sub":{"<name>":["Name1", Name2"]}}

MailMessage m = new MailMessage("[email protected]", "[email protected]");
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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET