简体   繁体   中英

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. The reason is that the JSON content of the script will be read by a 3rd party and parsed.

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. 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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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