我怎么能编码url的参数?

时间:2016-10-17 02:18:13

标签: c# asp.net-mvc-4

我尝试在网址中对参数email进行编码,为此我尝试使用HttpUtility.HtmlEncode,但由于参数不是&#39而无法正常工作; t编码。我怎么能这样做?

尝试

private void sendMailToValidation(String email){ 
            //String server = HttpContext.Current.Request.Url.Host;
            //encripta a url HttpUtility.HtmlEncode
            string absoluteURL = "http://" + HttpContext.Request.Url.Authority + "/Usuario/validaEmail?email=";
            try{
                MailMessage mail = new MailMessage();
                mail.To.Add(email);
                mail.From = new MailAddress("myemail@gmail.com");
                mail.Subject = "MeuPedido validação de email";
                string url = absoluteURL + HttpUtility.HtmlEncode(email);
                mail.Body = "<html><body><a href=\"" + url + "\">Test</a></body></html>"; 
                mail.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.UseDefaultCredentials = false;                
                smtp.Credentials = new System.Net.NetworkCredential("myemail@gmail.com", "xxxx");// Enter seders User name and password
                smtp.EnableSsl = true;
                smtp.Timeout = 20000;
                smtp.Send(mail);
            }catch (SmtpException e){
                Console.WriteLine(e.StackTrace);
            }
        }

0 个答案:

没有答案