当您使用System.Net.MailMessage动态发送电子邮件时,为什么不需要指定任何编码?

发布于 2024-09-27 18:24:41 字数 125 浏览 3 评论 0原文

根据我的模糊理解,任何文本都以字节流的形式在互联网上传输。当您将文本更改为字节或从字节更改时,您需要编码。 MailMessage.Body 只是一个纯字符串(文本),它作为电子邮件通过互联网发送。为什么不用指定编码就能正确显示汉字呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木緿 2024-10-04 18:24:41

简单的答案 - 如果正文中的任何字符不支持 ASCII(Chr(127) 以上),则编码类型为 UTF-8,否则编码为 ASCII。以下是 Reflector 中 body 属性的反汇编:

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

以下是 IsAscii 函数的内容...

 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

当然,可以通过手动指定编码来覆盖此行为。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文