skip to Main Content

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

2

Answers


  1. 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

    Login or Signup to reply.
  2. 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".)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search