简体   繁体   English

如何防止在MailMessage正文中将加号编码为ASCII代码

[英]How can I prevent the plus sign from being encoded to ASCII code in body of MailMessage

I'm trying to embed some javascript tags in a MailMessage that is being sent out from our system. 我正在尝试将一些javascript标记嵌入从系统发出的MailMessage中。 The reason is that the JSON content of the script will be read by a 3rd party and parsed. 原因是脚本的JSON内容将由第三方读取并解析。

The script tag should be formatted like this: 脚本标签应采用以下格式:

<script type="application/json+trustpilot">

and this is how it looks when I examine the body property of the MailMessage. 这就是我检查MailMessage的body属性时的外观。 However, when it is delivered to the mail client, it has been formatted like so: 但是,当将其传递到邮件客户端时,其格式如下:

<script type="application/json&#43;trustpilot">

There is plenty of advice explaining on how to encode special characters in html, but none that explain how to stop it from happening in special cases such as the above. 关于如何在html中编码特殊字符,有很多建议可以解释,但是在上述特殊情况下,没有什么建议可以阻止这种情况的发生。 Can anyone help out, I've spend an entire day trying to figure this out! 谁能帮忙,我花了一整天的时间来解决这个问题!

Thanks 谢谢

I am not able to reproduce the issue you are experiencing. 我无法重现您遇到的问题。 I created a minimal console application in C# to try to reproduce it. 我用C#创建了一个最小的控制台应用程序以尝试重现它。 I am sending en email with only a very small "structured code snippet" in the body of the email. 我正在通过电子邮件正文发送仅带有非常小的“结构化代码段”的电子邮件。

namespace TestingMailMessage
{
    using System.Net;
    using System.Net.Mail;

    public class Program
    {
        public static void Main(string[] args)
        {
            const string FromEmail = "<some-email-address>";
            const string Password = "<your-password>";

            var mailMessage = new MailMessage(
                FromEmail,
                "xxxxxxxxxx@invite.trustpilot.com",
                "This is a test trigger",
                "<script type=\"application/json+trustpilot\">{\"recipientEmail\": \"jane.doe@trustpilot.com\"}</script>");

            var smtpClient = new SmtpClient
                {
                    Host = "smtp.gmail.com",
                    Port = 587,
                    EnableSsl = true,
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = false,
                    Credentials = new NetworkCredential(FromEmail, Password)
                };

            smtpClient.Send(mailMessage);
        }
    }
}

When the email is received, it looks correct - and the "structured data snippet" is parsed correctly. 收到电子邮件后,它看起来正确-并且正确解析了“结构化数据片段”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM