此处的基本问题:我正在使用 .NET 框架 (3.5) 的默认 SmtpClient 发送电子邮件。正文类型是 HTML (IsBodyHtml = true) 在正文中,我在查询字符串中添加了一个带有两个参数的 url,如下所示:
http://server.com/page.aspx?var1=foo&var2=bar
这个 get 被编码为:
http://server.com/page.aspx?var1=foo%26var2=bar (the ampersand is encoded as percent-26)
当执行一个简单的 Request["var2"]
时,我得到“null”。
我应该如何正确编码邮件消息中的 & 符号?
最佳答案
这对我来说很好:
var client = new SmtpClient();
client.Host = "smtp.somehost.com";
var message = new MailMessage();
message.From = new MailAddress("from@example.com");
message.To.Add(new MailAddress("to@example.com"));
message.IsBodyHtml = true;
message.Subject = "test";
string url = HttpUtility.HtmlEncode("http://server.com/page.aspx?var1=foo&var2=bar");
message.Body = "<html><body><a href=\"" + url + "\">Test</a></body></html>";
client.Send(message);
关于c# - 使用 SmtpClient 发送带有查询字符串的 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2279830/