2016-08-31 43 views
1

我使用mailgun發送電子郵件。電子郵件正文是包含查詢字符串參數的網址的html內容。在收到的電子郵件中,查詢字符串中的內容已打破「&」字符。我用標準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 

回答

0

發佈之前編碼的信息嘗試HTML。

「&」標誌着一個HTML字符

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

所以看起來,當瀏覽器看到的「&」它抓住它背後接下來的幾個字符,並試圖將其轉換成HTML一樣的開始字符,這將引發錯誤,因爲「& ID2 = x」不是有效的html字符代碼。爲了防止這種情況,嘗試加入這一行你的身體下方=「文本」行

Body = System.Net.WebUtility.HtmlEncode(Body) 

這將在你的身體串中的所有HTML特殊字符轉換成相對應的HTML代碼。如果您的電子郵件顯示爲html,則不應該有更多的錯誤。但是,如果發生電子郵件必須以純文本形式顯示或解釋的情況,那麼您需要使用HtmlDecode反轉該過程。

+0

嗨soohoonigan,謝謝你的回覆。我試過HTMLEncode,mailgun返回一個錯誤(壞請求)。 – user6780213

相關問題