views:

25

answers:

1

In my vague understanding, any texts are transferred over the internet as streams of bytes. And when you change texts to and from bytes, you need encoding. MailMessage.Body is just a plain string(text) and it gets sent over the internet as emails. Why is it that it can correctly display Chinese characters without even having to specify the encoding?

+2  A: 

Simple answer - if any character in the body is not supported by ASCII (above Chr(127)), the encoding type is UTF-8, otherwise the encoding is ASCII. Here is the disassembly of the body property from Reflector:

Public Property Body As String
    Get
        If (Me.body Is Nothing) Then
            Return String.Empty
        End If
        Return Me.body
    End Get
    Set(ByVal value As String)
        Me.body = value
        If ((Me.bodyEncoding Is Nothing) AndAlso (Not Me.body Is Nothing)) Then
            If MimeBasePart.IsAscii(Me.body, True) Then
                Me.bodyEncoding = Encoding.ASCII
            Else
                Me.bodyEncoding = Encoding.GetEncoding("utf-8")
            End If
        End If
    End Set
End Property

And here are the contents of the IsAscii function...

 Friend Shared Function IsAscii(ByVal value As String, ByVal permitCROrLF As Boolean) As Boolean
    If (value Is Nothing) Then
        Throw New ArgumentNullException("value")
    End If
    Dim ch As Char
    For Each ch In value
        If (ch > ChrW(127)) Then
            Return False
        End If
        If (Not permitCROrLF AndAlso ((ch = ChrW(13)) OrElse (ch = ChrW(10)))) Then
            Return False
        End If
    Next
    Return True
End Function

Of course, this behavior can be overridden by manually specifying an encoding.

NightOwl888
Wow I didn't know Reflector was that powerful! Regarding this particular encoding-decoding process, does that mean the actual encoding method does not matter as long as the encoding matches with decoding?
Aperture
That is correct. All you are doing at this stage is specifying an encoding to use for the message. After calling SmtpClient.Send(), the mail server that is associated with SmtpClient will then add the appropriate headers when sending the message out on the internet. The receiving server will read the headers and decode the message appropriately.
NightOwl888