2016-08-31 7 views
1

私はmailgunを使用してメールを送信しています。電子メール本文は、クエリ文字列パラメータを含むURLを持つ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を試してみてください。ここで

は、サンプルコードです。

は「&」は

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

だから、それはブラウザが「&」それはその背後にある次の数文字をつかみ、HTMLにそれを翻訳しようとしている見ているときのように見えるHTML文字の始まりを知らせます"& ID2 = x"は有効なhtml文字コードではないため、エラーが発生します。これを防ぐために、それらに対応するHTMLコードにあなたの体の文字列内のすべてのHTMLの特殊文字を変換しますあなたの体=「text」の行

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

の下にこの行を追加してみてください。電子メールがhtmlとして表示されている場合、エラーはなくなります。ただし、電子メールをプログラムでプレーンテキストとして表示または解釈する必要がある場合は、HtmlDecodeを使用してプロセスを元に戻す必要があります。

+0

こんにちはsoohoonigan、返信いただきありがとうございます。私はHTMLEncodeを試しました、エラー(悪い要求)で返すメールガン。 – user6780213

関連する問題