Send url with querystring with SmtpClient

The main question here is: I send emails using the standard SmtpClient .NET platform (3.5). The body type is HTML (IsBodyHtml = true). In the body, I added a URL with two parameters in querystring as follows:

http://server.com/page.aspx?var1=foo&var2=bar 

This is encoded for:

 http://server.com/page.aspx?var1=foo%26var2=bar (the ampersand is encoded as percent-26) 

When I execute a simple Request["var2"] I get "null". What should I do to correctly encode the ampersand in the mail?

+4
source share
2 answers

This works fine for me:

 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); 
+3
source

Use UrlEncode . This will do the whole encoding of your input string for you.

0
source

Source: https://habr.com/ru/post/1301558/


All Articles