简体   繁体   中英

Sending HTML Email using Mailgun API with RestSharp C# causing message to be truncated at ampersand “&” when included

Below is the code I am currently running in a console app to test. The email is sent successfully.

But the body is truncated and you only see This is test HTML without & rest of message . I can workaround by replacing & with and but this does not work where I need to embed urls in emails with query string parameters.

I have tried html encoding without any luck.

var client = new RestSharp.RestClient("https://api.mailgun.net/v3");
client.Authenticator = new RestSharp.HttpBasicAuthenticator("api", "{APIKEY}");

RestSharp.IRestRequest request = new RestSharp.RestRequest("/{DOMAIN}/messages", RestSharp.Method.POST);

string MailBody = "<html>This is test HTML & rest of message</html>";

request.AddParameter("from", "{EmailAddress}");
request.AddParameter("h:Reply-To", "{EmailAddress}");
request.AddParameter("to", "{EmailAddress}");
request.AddParameter("subject", "Mailgun Test New");

request.AddParameter("html", MailBody);

try
{
    RestSharp.IRestResponse response = client.Execute(request);
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

I replaced the & with

&amp;

in the text and it worked.
In links I just kept the & as is and it worked to.

string MailBody = "<html><body>This is test HTML &amp; rest of message. <a href=\"http://www.google.com?a=b&c=d\">Link with querystring</a></body></html>";

The issue was was with the version of the RestSharp full I was using. I updated the project to use the latest and everything worked as expected.

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