尝试发送链接与查询字符串作为电子邮件

时间:2011-01-13 16:43:42

标签: asp.net vb.net

 Dim x As String
 x = "http://www.domain.com/aaa/test/default2.aspx?date=" & now.Text & "&tfname=" & p1fname.Text & "&tlname=" & p1lname.Text & "&comp=" & Request.QueryString("comp")

 Dim objEmail As New MailMessage()
 objEmail.To = "test@email.com"
 objEmail.From = "me@test.com"
 objEmail.Cc = "test@email.com"
 objEmail.Subject = "Test Email"
 objEmail.Body = x
 SmtpMail.SmtpServer = "mail.domain.com"
 Try
     SmtpMail.Send(objEmail)
 Catch exc As Exception
     Response.Write("Send failure: " + exc.ToString())
 End Try

当我收到电子邮件时

http://www.domain.com/aaa/test/default2.aspx?date=1/13/2011

作为链接

其余为文本

11:39:09 AM& tfname = sadasd& tlname = asd& comp = GWI

2 个答案:

答案 0 :(得分:3)

每当您将参数放入查询字符串时,您应该使用System.Web.HttpUtility.UrlEncode对其进行编码,以避免无效字符进入URL:

x = "http://www.domain.com/aaa/test/default2.aspx?date=" & HttpUtility.UrlEncode(now.Text) &
    "&tfname=" & HttpUtility.UrlEncode(p1fname.Text) &
    "&tlname=" & HttpUtility.UrlEncode(p1lname.Text) &
    "&comp=" & HttpUtility.UrlEncode(Request.QueryString("comp"))

答案 1 :(得分:2)

如果需要放置空格,则在查询字符串中不能包含空格,在追加到查询字符串之前将其替换为%20。虽然这样做的理想方法是加密和解析查询字符串中的文本。