I'm using SendGridPlus API for sending e-mails with SendGrid. Everything works fine. But I have a problem with spanish characters like á
or é
when I include them in substitution values in the header.
For instance if I include Que día más bueno hace
I receive Que d?a m?s bueno hace
.
Here is the code:
[Test]
public void TestSendGrid()
{
//create a new message object
var message = Mail.GetInstance();
var recipients = new List<string>();
//You could loop through your dataset here and add each recipient, up to 1000 recipients per message
recipients.Add("[email protected]");
recipients.Add("[email protected]");
//We want to add the recipients to the X-SMTPAPI header
message.Header.AddTo(recipients);
//Even though we added recipients to the Header, the envelope must also have a valid recipient
message.AddTo("[email protected]");
//set the sender
message.From = new MailAddress(From);
//set the message body
message.Html = "<html><p>Hello *|USERNAME|*</p><p>World *|MESSAGE|* </p></html>";
//This replacement key must exist in the message body
var replacementKey = "*|USERNAME|*";
//There should be one value for each recipient in the To list
var substitutionValues = new List<string> { "Mr Foo", "Mrs Raz" };
message.AddSubVal(replacementKey, substitutionValues);
//This replacement key must exist in the message body
replacementKey = "*|MESSAGE|*";
//There should be one value for each recipient in the To list
substitutionValues = new List<string> { "Que día más bueno hace", "ea4t1qj8BnU%3d" };
message.AddSubVal(replacementKey, substitutionValues);
//set the message subject
message.Subject = "Hello World HTML Test";
//create an instance of the SMTP transport mechanism
var transportInstance = SMTP.GetInstance(new NetworkCredential(Username, Password));
//send the mail
transportInstance.Deliver(message);
}
Could anybody please give offer advice on how to avoid this?
Here is the email header after using unicode characters.
> Delivered-To: [email protected] Received: by 10.194.13.129
> with SMTP id h1csp46752wjc;
> Fri, 14 Dec 2012 13:49:55 -0800 (PST) Received: by 10.182.89.103 with SMTP id bn7mr5984026obb.35.1355521795301;
> Fri, 14 Dec 2012 13:49:55 -0800 (PST) Return-Path: <bounces+50334-7e69-juanp_perez=loyaltycrm.com@email.loyaltycrm.com>
> Received: from o1.email.loyaltycrm.com (o1.email.loyaltycrm.com.
> [208.117.60.213])
> by mx.google.com with SMTP id kq7si6188156obb.180.2012.12.14.13.49.54;
> Fri, 14 Dec 2012 13:49:55 -0800 (PST) Received-SPF: pass (google.com: domain of
> bounces+50334-7e69-juanp_perez=loyaltycrm.com@email.loyaltycrm.com
> designates 208.117.60.213 as permitted sender)
> client-ip=208.117.60.213; Authentication-Results: mx.google.com;
> spf=pass (google.com: domain of
> bounces+50334-7e69-juanp_perez=loyaltycrm.com@email.loyaltycrm.com
> designates 208.117.60.213 as permitted sender)
> smtp.mail=bounces+50334-7e69-juanp_perez=loyaltycrm.com@email.loyaltycrm.com;
> dkim=pass [email protected] DKIM-Signature: v=1; a=rsa-sha1;
> c=relaxed; d=sendgrid.info; h=
> mime-version:from:to:subject:content-type
> :content-transfer-encoding; s=smtpapi; bh=VEWUIUwwWzV7+YxR73tT5c
> ppXfY=; b=bccVTLSqyIp8og5+IMAAwcZk54FnLw8k38X9/rNwRqVUTgj+CZQDwD
> LOujhu+HDnYfZXOf78dugxcg5ScWNiDzbuEh6vr3xy9jNEiwkL+yiplayfWeXwGx
> r63P7qXjhADkfn/nDeca5QkMN08Pf3+4XFWTJiRHqG0T2hGOjwxZM=
> DomainKey-Signature: a=rsa-sha1; c=nofws; d=loyaltycrm.com; h=
> mime-version:from:to:subject:content-type
> :content-transfer-encoding; q=dns; s=smtpapi; b=PC8I8GCSsZbqJBBW
> i3mz2jhVy2lxTUqqinzsTu/GMl725muTRKS32KpQioE6iHIMm1thHbtrFP19DSiq
> edJhQ5BKfCNZKrxT72c9DS1/lSl96euCqEsIbHA3b/PWl7OxgoVCw3FdOGljssh7
> lQQLGeBIh3HbYo1yL1huXbG72Js= Received: by 10.4.35.208 with SMTP id
> mf73.16152.50CB9EF79
> Fri, 14 Dec 2012 15:49:43 -0600 (CST) Received: from LCRM1 (unknown [10.60.208.13]) by None (SG) with ESMTP id
> 50cb9ef7.7fc9.381e109 for <[email protected]>; Fri, 14 Dec
> 2012 15:49:43 -0600 (CST) MIME-Version: 1.0 From: [email protected]
> To: [email protected] Date: 14 Dec 2012 22:49:40 +0100
> Subject: Hello World HTML Test Content-Type: text/html;
> charset=us-ascii Content-Transfer-Encoding: quoted-printable
> Message-ID: <[email protected]>
> X-Sendgrid-EID:
> gCyESDQLdMYAVtS85w9Vrx3WCa1D47LsR1aGBtau8RBzc10shTwXzozJNPYySyl9DqFygFiq8Fky1nQzp1S/n1d/BrVpf0HPjWlFU0UghfqTLmqRf+7tZknCIg1mkVq0
<html><p>Hello Mr Foo</p><p>World Que d\u00EDa m\u00E1s bueno hace </p></html>
<img src=3D"http://email.loyaltycrm.com/wf/open?upn=3DAMeQx0pEtkpR4-2FebJhh=
IV-2FlVQNg-2BgGs-2FqnbUO1ME7JjAjdPdI5TbxawOrUSlu-2B8QnxtPMS9mOdHRwby4N4j1B1=
YJZARjusRG5pNHPBQW6tTE0gcAX6G4HdYpZXXrySEYSn5w-2FxuNG1Wm3UUoOOwRFH-2FxLn2CF=
F4MD17CYSTJ6biVBbVijCX7rKc9RM-2FIErLm" alt=3D"" width=3D"1" height=3D"1" bo=
rder=3D"0" style=3D"height:1px !important;width:1px !important;border-width=
:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-rig=
ht:0 !important;margin-left:0 !important;padding-top:0 !important;padding-b=
ottom:0 !important;padding-right:0 !important;padding-left:0 !important;"/>
By default (and RFC) SMTP headers are US-ASCII. In order to send non-ASCII characters, you need to use Unicode escape sequences. I used the following string and it worked.
Que d\u00EDa m\u00E1s bueno hace
You can find a function in C# to automatically escape those characters for you: Convert a Unicode string to an escaped ASCII string
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With