Reputation: 1283
I am sending emails via a VB application. The following characters displays correctly on some email applications but on others, I get a weird replacement (See attached image). How can I go about making these characters always display correctly?
The character set : ( ) & * % $ # @ ! ~ ; _ = + / - ?
The Message : 18cm – 22cm Wide; Fat O
The result :
No matter which one of the characters gets used, The result is always the same as the attached image. The applications that does not display correctly is outlook < 2013.
Here is the VB code:
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 = "[email protected]"
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("[email protected]", "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
Upvotes: 1
Views: 900
Reputation: 3805
Firstly, The –
in 18cm – 22cm Wide; Fat O
is a Unicode em-dash, not a hypen/minus sign. So your character set contains more than you think.
It's obviously a UTF-8 encoding problem. That's what you get if you display UTF-8-encoded text in Latin-1. Some MUAs have different defaults, or apply heuristics, when a specific character set is not declared.
There is an easy enough fix for this specific problem. Instead of giving a content-type of text/html
, give text/html; charset=UTF-8
.
Note that this hints at confusion about encodings in your database database and/or codebase if you've never seen this problem before, and it's a right pain to sort out. Consider fixing it to be a learning experience :)
Upvotes: 2