发送html邮件在某些应用程序上正确显示但在其他应用程序上没有

时间:2013-08-20 11:26:07

标签: html vb.net email

我正在通过VB应用程序发送电子邮件。以下字符在某些电子邮件应用程序中正确显示,但在其他电子邮件应用程序中,我得到了一个奇怪的替代品如何才能使这些字符始终正确显示?

字符集:()& *%$#@! 〜; _ = + / - ?

消息:18厘米 - 22厘米宽;胖子

结果:enter image description here

无论使用哪一个字符,结果始终与附加图像相同。未正确显示的应用程序是outlook< 2013。

这是VB代码:

Sub subHtmlEmail(ByVal strAddresseeEmail As String, ByVal strGroup As String)
    Try
        Dim strTo, strFrom, strBody, strSubject, strBcc As String
        Dim boolHtml As Boolean = True ' set the body content to plain text(false) or HTML(true)
        strFrom = "sales@humeat.com"
        strTo = strAddresseeEmail ' ; Separates emails
        strBcc = "" ' ; Separates emails

        strSubject = txtEmailSubject.Text

        strBody = "<html><head></head><body>"
        strBody = strBody & "<img src=cid:Logo>"
        strBody &= "<br><br>"


        strBody &= "Dear " & clsGroupCustomers.RetrieveAddressee(strAddresseeEmail, strGroup) & ",<br><br>"

        Dim strLines As String() = txtBody.Text.Split(New [Char]() {CChar(vbCrLf)})
        For Each strLine As String In strLines
            strBody &= strLine & "<br>"
        Next

        strBody &= "<br><br>"

        Dim strFooterLines As String() = txtFooter.Text.Split(New [Char]() {CChar(vbCrLf)})
        For Each strFooterLine As String In strFooterLines
            strBody &= strFooterLine & "<br>"
        Next

        HTMLView = AlternateView.CreateAlternateViewFromString(strBody, Nothing, "text/html")

        strBody &= "</body></html>"

        subEmail(strFrom, strTo, strBcc, strSubject, strBody, boolHtml, strAddresseeEmail)
        'subEmail(strFrom, strTo, strBcc, strSubject, System.Web.HttpUtility.HtmlEncode(strBody), boolHtml, strAddresseeEmail)

    Catch ex As Exception
        Cursor = Cursors.Default
        MsgBox("An error has occurred in your application while attempting to create the email." & Chr(13) & Chr(13) & "Description: " & ex.Message & Chr(13) & Chr(13) & "Please contact your System Administrator.", MsgBoxStyle.Critical, "Application Error")
        Exit Sub
    End Try
End Sub

'Send the email
Sub subEmail(ByVal strFrom, ByVal strTo, ByVal strBcc, ByVal strSubject, ByVal strBody, ByVal bolHtml, ByVal strAddresseeEmail)
    Try
        'Dim strMailServer As String = "smtp.dsl.telkomsa.net"
        Dim strMailServer As String = "smtp.insightsa.net"
        'Dim strMailServer As String = "smtp.humeat.com"
        'Dim strMailServer As String = "mail.humeat.com"

        Dim intCount As Integer
        Dim objAttachment As Attachment

        Dim objMail As New MailMessage()
        objMail.From = New MailAddress(strFrom)
        Dim i As Integer
        Dim arrArray As Array

        arrArray = Split(strTo, ";")
        For i = 0 To arrArray.Length - 1
            objMail.To.Add(arrArray(i))
        Next

        arrArray = Split(strBcc, ";")
        For i = 0 To arrArray.Length - 1
            If Not arrArray(i) = "" Then objMail.Bcc.Add(arrArray(i))
        Next

        For intCount = 0 To lstAttachments.Items.Count - 1
            objAttachment = New Attachment(lstAttachments.Items(intCount).ToString)
            objMail.Attachments.Add(objAttachment)
        Next


        ' [TW 20110309]
        ' Create the LinkedResource (embedded image)
        Dim logo As New LinkedResource("C:\humeat.bmp")
        logo.ContentId = "Logo"

        ' [TW 20110309]
        ' Add the LinkedResource to the appropriate view
        HTMLView.LinkedResources.Add(logo)

        ' [TW 20110309]
        ' Add the views
        objMail.AlternateViews.Add(PlainView)
        objMail.AlternateViews.Add(HTMLView)

        objMail.Subject = strSubject
        objMail.Body = strBody
        objMail.IsBodyHtml = bolHtml
        Dim smtp As New SmtpClient(strMailServer)
        smtp.Port = "587" ' This is not the default port of 25 but a special smtp port because they use Mweb.  HC. 2-8-2011


        smtp.Credentials = New System.Net.NetworkCredential("humeat@insightsa.net", "123Four56")
        smtp.Send(objMail)

    Catch ex As Exception
        Cursor = Cursors.Default
        'MsgBox("An error has occurred in your application while attempting to send the email to " & strTo & "." & Chr(13) & Chr(13) & "Description: " & ex.Message & Chr(13) & Chr(13) & "Please contact your System Administrator.", MsgBoxStyle.Critical, "Application Error")
        lstEmailsNotSent.Items.Add(clsGroupCustomers.RetrieveAddressee(strAddresseeEmail, cboEmailGroup.Text) & " (" & strTo & ") - " & ex.Message)
        Exit Sub
    End Try
End Sub

1 个答案:

答案 0 :(得分:2)

首先,中的18cm – 22cm Wide; Fat O是Unicode em-dash,而不是超/减号。所以你的角色集比你想象的要多。

这显然是一个UTF-8编码问题。如果您在Latin-1中显示UTF-8编码的文本,那就是您所获得的。当未声明特定字符集时,某些MUA具有不同的默认值或应用启发式。

对于这个特定问题,有一个足够简单的解决方法。不要给出text/html的内容类型,而是text/html; charset=UTF-8

请注意,如果您之前从未见过这个问题,这会暗示您对数据库数据库和/或代码库中的编码存在困惑,这是一个很难理解的问题。考虑将其修复为学习体验:)