使用带有RestSharp C#的Mailgun API发送HTML电子邮件导致消息在&符号时被截断“&”

问题描述 投票:0回答:2

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

但身体被截断,你只看到没有This is test HTML& rest of message。我可以通过替换&来解决这个问题,但是这不适用于需要在带有查询字符串参数的电子邮件中嵌入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);
}
c# api html-email restsharp mailgun
2个回答
0
投票

我替换了&

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

0
投票

问题在于我正在使用的RestSharp版本。我更新了项目以使用最新的,一切都按预期工作。

© www.soinside.com 2019 - 2024. All rights reserved.