When you use System.Net.MailMessage to dynamically send emails, why aren't you required to specify any encoding?
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 plai开发者_如何学JAVAn 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?
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.
精彩评论