繁体   English   中英

使用Mailgun API和RestSharp C#发送HTML电子邮件,如果包含,则导致邮件被截断为“&”

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

下面是我当前正在控制台应用程序中测试的代码。 电子邮件发送成功。

但是正文被截断了,您只会看到This is test HTML没有& rest of message This is test HTML 我可以通过用&替换为&来解决,但这不适用于需要在带有查询字符串参数的电子邮件中嵌入urls地方。

我尝试了HTML编码,但没有任何运气。

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

我将&替换为

&amp;

在文本中,它起作用了。
在链接中,我只是按原样保留&,并且一直有效。

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

问题出在我正在使用的RestSharp完整版本上。 我更新了该项目以使用最新版本,并且一切正常。

暂无
暂无

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

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