How to make characters displayed correctly when sending email with chilkat?

402 Views Asked by At

I use chilkat 9.5.0.80.

The problem is that some characters in email appear wrong. For example this è appears as è in the inbox mailbox. I expect it to be displayed as I set it - è.

This is the minimal reproducible code:

email := chilkat.NewEmail()
email.SetCharset("utf-8")
email.AddTo("", toEmail)
email.SetSubject("test")
email.SetHtmlBody("è")
mailMan := chilkat.NewMailMan()
mailMan.SetSmtpHost(host)
mailMan.SetSmtpPort(25)
mailMan.SetSmtpUsername(username)
mailMan.SetSmtpPassword(password)
mailMan.SendEmail(email)

Also I noticed that the header of html body part in different smtp servers mime appears different. First is displayed correctly and looks like:

--------------050209010604000502060206
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset=iso-8859-1

And second is wrong, looks like:

--------------030709010607010008060507
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset=utf-8

The charset is different..

I think if I would set the html body as base64 it will displayed correct. But I have not found such possibility in chilkat...

Also tried with valid html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
è
</body>
</html>
1

There are 1 best solutions below

0
On

I was right when I asked in my question, how to set html body as base64 with chilkat ? Because the following code shows special characters correctly on some smtp servers, but on some servers they are displayed wrong:

email := chilkat.NewEmail()
email.SetCharset("utf-8")
email.AddTo("", toEmail)
email.SetSubject("test")
email.SetHtmlBody("è")
mailMan := chilkat.NewMailMan()
mailMan.SetSmtpHost(host)
mailMan.SetSmtpPort(25)
mailMan.SetSmtpUsername(username)
mailMan.SetSmtpPassword(password)
mailMan.SendEmail(email)

The next solution allows to set and send html body mime part as base64, and it is displayed correctly on different smtp servers:

email := chilkat.NewEmail()
part := chilkat.NewMime()
part.SetBodyFromEncoded("base64", 
base64.StdEncoding.EncodeToString([]byte(bodyContent)))
part.SetContentType("text/html")
part.SetCharset("UTF-8")
email.SetFromMimeText(*part.GetMime())

Then you can set other fields to email object such as subject etc..