开发者

MailMessage.Body loses CrLf with Encoding.UTF8

开发者 https://www.devze.com 2023-02-13 00:37 出处:网络
I had an interesting bug today when sending plain text emails from our system.We format messages like this:

I had an interesting bug today when sending plain text emails from our system. We format messages like this:

-1st something happened blab bla.

-2nd something else happened blab blab blaa bla.

Today we had an email that looked like this:

-1st something happened blab bla.

-2nd something else happened blab blab blaa bla $1000.00 -3rd Something happened.

So above, we see that we lost the CrLf but only on the message that didn't have a period and ended in 0. I went through the code and found the CrLf is intack up until we send the email. I tracked the code down below, I am guessing it applies to C# as well:

NOTE: We use Environment.NewLine when building the string for the body.

Building the string:

If Not errorList Is Nothing Then
    If errorList.Count > 0 Then
        strBldrBody.Append(EMailHe开发者_JS百科lper.CrLf)
        strBldrBody.Append(EMailHelper.CrLf)
        strBldrBody.Append("Response Error List:")
        For Each itm As String In errorList
         strBldrBody.Append(EMailHelper.CrLf)
         strBldrBody.Append(DataHelper.DASH)
         strBldrBody.Append(itm)
        Next
    End If
End If

Email encoding setting:

Try
    If Not String.IsNullOrEmpty(recipient) Then
        Using mailMsg As MailMessage = New MailMessage()

            mailMsg.From = New MailAddress(_configHelper.EmailFrom)
            mailMsg.Subject = subject
            mailMsg.Body = body
            mailMsg.BodyEncoding = Encoding.UTF8

            EMailHelper.SetToAddress(recipient, mailMsg)   

            Dim smtpClient As SmtpClient = New SmtpClient(_configHelper.EmailRelayServer)

            smtpClient.Send(mailMsg)
        End Using
    End If

Catch ex As System.Exception
    'logs error
End Try

I want to know what happened in the string translation during the UTF-8 encoding/decoding that removes the CrLf?!

The problem was Outlook, see below: outlook screen shot


We just had a problem similar to this, but it turned out to be a conversion we did from a bytestream to a string. I have also seen this happen when getting encodings mixed up. From the code posted, I don't see how this could happen.

Oh, and to Hans - he meant 'intact' not 'in tack'


You say that the message is intact (including that newline) up to the point where you send the message, but when the message was received it didn't contain the newline? Can you duplicate this error?

Whereas it's possible that the SmtpClient control somehow lost the newline, it's also possible that one of the relay servers lost it in translation somewhere, or that your email client didn't render it correctly.

Messages go through an incredible number of different translation steps between servers, and some of those translations are ... less than rigorously debugged. In particular, the quoted-printable encoding has some interesting edge cases that lots of implementations don't get correct.

If your relay server is local, I would suggest turning on logging so that it will save a copy of the message as it's received from you. And on the receiving end, get the actual bits of the message (if possible), without any kind of translation by client software.

0

精彩评论

暂无评论...
验证码 换一张
取 消