发送URL与SmtpClient查询字符串
-
21-09-2019 - |
题
在这里基本问题:我在使用.NET Framework(3.5)的默认SmtpClient发送电子邮件。的体形是在身体我在查询字符串添加一个URL具有两个参数,像这样的HTML(IsBodyHtml =真):
http://server.com/page.aspx?var1=foo&var2=bar
此获取的已编码为:
http://server.com/page.aspx?var1=foo%26var2=bar (the ampersand is encoded as percent-26)
在做一个简单的Request["var2"]
我得到“空”。
我应该怎么做才能正确编码的邮件消息的符号?
解决方案
这正常工作对我说:
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);
其他提示
使用以UrlEncode 方法。这将做你的输入字符串的所有编码为你。
不隶属于 StackOverflow