在C#中使用X-SMTPAPI的Sendgrid字符编码标头

时间:2012-12-14 06:27:32

标签: c# sendgrid

我正在使用SendGridPlus API与SendGrid发送电子邮件。一切正常。但是当我将它们包含在标题中的替换值中时,我遇到了áé等西班牙语字符的问题。

例如,如果我包含Que día más bueno hace,我会收到Que d?a m?s bueno hace

以下是代码:

       [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);        
    }

有人可以提供关于如何避免这种情况的建议吗?

以下是使用unicode字符后的电子邮件标题。

> 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;"/>

1 个答案:

答案 0 :(得分:2)

默认情况下(和RFC)SMTP标头是US-ASCII。要发送非ASCII字符,您需要使用Unicode转义序列。我使用了以下字符串,它起作用了。

Que d\u00EDa m\u00E1s bueno hace

您可以在C#中找到一个功能来自动转义这些字符:Convert a Unicode string to an escaped ASCII string