7

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("juanp_perez@loyaltycrm.com");
        recipients.Add("juanp_perez@msn.com");

        //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("juanp_perez@loyaltycrm.com");

        //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: juanp_perez@loyaltycrm.com 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 header.i=@sendgrid.info 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 <juanp_perez@loyaltycrm.com>; Fri, 14 Dec
> 2012 15:49:43 -0600 (CST) MIME-Version: 1.0 From: info@loyaltycrm.com
> To: juanp_perez@loyaltycrm.com 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: <1355521783.554221818170987@mf73.sendgrid.net>
> 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;"/>
bwest
  • 9,182
  • 3
  • 28
  • 58
peelmicro
  • 71
  • 2
  • 4

1 Answers1

3

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

Community
  • 1
  • 1
bwest
  • 9,182
  • 3
  • 28
  • 58
  • Brand, thank you very much for your answer. I tried it, changing in my code 'Que día más bueno hace' by 'Que d\u00EDa m\u00E1s bueno hace' but I am still receiving 'Que d?a m?s bueno hace'. – peelmicro Dec 14 '12 at 20:33
  • I have just tested several times in C# and can't reproduce the issue. Can you provide the raw headers and body from the email where the characters are not displaying correctly? – bwest Dec 14 '12 at 22:08
  • Brand, I updated the post to include the header. The problem now is that I receive 'Que d\u00EDa m\u00E1s bueno hace' – peelmicro Dec 15 '12 at 10:39
  • Here is the problem: `charset=us-ascii`, you should use utf-8. I'll see if I can find how to set that in C#. After you fix this header you may not even need to use the escape sequences, though they should work. – bwest Dec 16 '12 at 01:39
  • Brand. I've solved the issue, I've used WebUtility.HtmlEncode to transform the characters ant it works great. Thank you very much for your time. – peelmicro Dec 17 '12 at 08:22
  • 2
    Cool, good work @Brand. Can we mark this as resolved then for future reference? – Swift Dec 18 '12 at 19:42