0

I wrote a java code to send a html eMail. For this I use jakarta.mail.

For the first try I got wrong characters in my eMail Client. I could fix it by using following code:

htmlPart.setContent(new String(mailTextContent.getBytes("UTF8"),"ISO-8859-1"), "text/html");

after using this code, on my development computer (Ubuntu) the mail works pretty fine. What I got can you pls. see in following, snipped which has the right encoding:

enter image description here German ä,ö and Euro (€) is correct.

Now I deployed the jar file to my ubuntu server (docker) After deploying I get following picture: enter image description here

In the sourcecode of the wrong eMail is following included:

    ------=_Part_0_1847509784.1659876554470
Content-Type: text/html; charset=ANSI_X3.4-1968
Content-Transfer-Encoding: quoted-printable

In the sourcecode of the correct eMail is following included:

    ------=_Part_0_2050835901.1659876395597
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

In both cases the client is the same. It looks like, that on my ubuntu server is an other behavior like on my ubuntu client

My html code starts with:

<!DOCTYPE html>
<html lang="de">
<head>
  <title>mailtest</title>
  <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"> 
</head>
<body>
</b>
<table border=''1''....

Is there any idea/hint how can I prevent such an impact? Thank you in advance

Schwabenheinz
  • 133
  • 1
  • 11

2 Answers2

0

Very likely your Ubuntu desktop system and your Ubuntu Server do not use the same language settings or default encoding settings. Whatever the OS settings, you can specify the default encoding for the JVM by simply setting a system property at startup:

java -Dfile.encoding=UTF8 au.com.objects.MyClass

On top of that your application should be able to set the encoding for the email body when constructing/sending the email.

See also Java, default encoding

Queeg
  • 7,748
  • 1
  • 16
  • 42
  • Thank you for sharing this! I thought about this possibility, but did not investigate at first, because I thought also that this could be an opportunity if I can not fix it by code. – Schwabenheinz Aug 08 '22 at 16:10
0

Your minimalistic DOCTYPE declares HTML 5 for its content. The HTML 5 way to specify a charset is (also brieve):

<meta charset="UTF-8">

It seems the binary bytes are in UTF-8 with multi-byte sequences for special characters, and send declared as an extended ASCII.

There is the global setting, a System property:

mail.mime.charset=UTF-8

System.setProperty("mail.mime.charset", "UTF-8");

I would search for the right spot for a declarative configuration.

For individual mails you can use with the MimeMessage:

message.setSubject("Ernährung/Körperpflege", "UTF-8");
message.setText("Nutraĵoj ĝenerale sanaj", "UTF-8");

I also saw a difference between decimal point and decimal comma. Hence the Locale defaults to the computer. Your development computer uses probably "GERMANY (de-DE) whereas the docker platform probably uses US.

(And then for a thousands separator using a MessageFormat would have been nice, even if it is just a non-breaking space "\u00a0".)

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • Thank you for your help! I checked the html code by using validator: [link]https://validator.w3.org. smal changes, but did not change the situation. As second I set the System property like you suggested. This did 50% of the trick ;-) The € sign for the currency was correct after this. But the others like ä are changed only a little bit from: ErnÃ?hrung/Körperpflege to Ern�?hrung/Körperpflege. Nice was, that ö was suddenly right. – Schwabenheinz Aug 08 '22 at 16:22
  • And also the source code of the eMail changed to: `------=_Part_0_1847509784.1659975294909 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable` – Schwabenheinz Aug 08 '22 at 16:24
  • Weird multiple chars to weird multiple chars is a double mix-up. A hard-coded string literal ion a java source could have compiled with the wrong encoding, differing from the encoding of the editor. The wrong source could also be from the database. And wrong conversion between Unicode texxt (`String`) and binary data (`byte[]`): no `new Stirng(bytes)` and no `string.getBytes()` - missing explicit charset parameter. And somewhere UTF-8 data is reinterpreted as (probably) Latin1. – Joop Eggen Aug 08 '22 at 20:08