mailgun电子邮件在查询字符串(电子邮件正文)中以&符号中断

时间:2016-08-31 19:07:28

标签: vb.net mailgun

我使用mailgun发送电子邮件。电子邮件正文是html内容,其中包含带有查询字符串参数的URL。在收到的电子邮件中,内容已经打破了"&"查询字符串中的字符。我正在使用带有标准httpwebrequest的vb.net。

在此感谢任何帮助。

以下是示例代码:

Try
        Body = "<br/>Please take a moment to review and sign your payment.</p><p><a href='http://test.com/signme.aspx?ID=xxxxxxxx&ID2=xxxxxx=='>&nbsp;&nbsp;&nbsp;&nbsp;Click here to review and electronically sign the authorization</a></p>"
        Dim strDataToPost As String = String.Empty
        Dim myWebRequest As HttpWebRequest
        Dim myRequestStream As Stream
        Dim myStreamWriter As StreamWriter

        Dim myWebResponse As HttpWebResponse
        Dim myResponseStream As Stream
        Dim myStreamReader As StreamReader
        myWebRequest = WebRequest.Create(URL)
        ' Set the method to "POST" and the content type so the server knows to expect form data in the body of the request.
        With myWebRequest
            .Method = "POST"
            .ContentType = "application/x-www-form-urlencoded"
        End With
        myWebRequest.Credentials = New NetworkCredential("api", "mykey")
        strDataToPost = strDataToPost + "from=" + FromEmail
        strDataToPost = strDataToPost + "&to=" + ToEmail
        If Cc.Trim.Length > 0 Then strDataToPost = strDataToPost + "&cc=" + Cc
        If Bcc.Trim.Length > 0 Then strDataToPost = strDataToPost + "&bcc=" + Bcc
        strDataToPost = strDataToPost + "&subject=" + Subject
        strDataToPost = strDataToPost + "&html=" + HttpUtility.HtmlDecode(Body)

        ' Get a handle on the Stream of data we're sending to the remote server, connect a StreamWriter to it, 
        ' and write our data to the Stream using the StreamWriter.
        myRequestStream = myWebRequest.GetRequestStream()
        myStreamWriter = New StreamWriter(myRequestStream)
        myStreamWriter.Write(strDataToPost)
        'WriteToLogFile("Data :" & strDataToPost)
        myStreamWriter.Flush()
        myStreamWriter.Close()
        myRequestStream.Close()

        ' Get the response from the remote server.
        myWebResponse = myWebRequest.GetResponse()

        ' Get the server's response status?
        ' Just like when we sent the data, we'll get a reference to the response Stream, connect a StreamReader to the Stream and 
        ' use the reader to actually read the reply.
        myResponseStream = myWebResponse.GetResponseStream()
        myStreamReader = New StreamReader(myResponseStream)
        Dim strResult As String = myStreamReader.ReadLine()
        myStreamReader.Close()
        myResponseStream.Close()
        myWebResponse.Close()
    Catch ex As Exception
        WriteToLogFile("Error Occured:" & ex.Message)
    End Try

1 个答案:

答案 0 :(得分:0)

在发布消息之前尝试对您的消息进行html编码。

“&安培;”发出html字符开头的信号

(i.e. "&#36;" = "$")

所以看起来当浏览器看到“&amp;”时它抓住它后面的下几个字符,并尝试将其转换为html字符,这将引发错误,因为“&amp; ID2 = x”不是有效的html字符代码。为防止这种情况,请尝试在Body =“text”行

下添加此行
Body = System.Net.WebUtility.HtmlEncode(Body)

这会将您的正文字符串中的所有html特殊字符转换为相应的html代码。如果您的电子邮件显示为html,则不应再出现错误。但是,如果您的程序必须以纯文本形式显示或解释电子邮件,则需要使用HtmlDecode撤消该过程。