Search code examples
utf-8character-encodingjakarta-mail

German letters in From name not shown with Java Mail


I'm sending mail with Java Mail. I use the following to set the sender info:

msg.setFrom(new InternetAddress("[email protected]", "Schaltfläche"));

Problem: When I send this message to my GMail, the sender is shown as Schaltfl?che.

In the source it's:

From: "=?ANSI_X3.4-1968?Q?Schaltfl=3Fche?=" <[email protected]>

Which looks...ok? At least it appears effort has been done to encode the ä.

So, what am I doing wrong? I could blame GMail, but that's a stretch, and testers are also seeing the error in other clients.

(Related but unrelated: The same name appears fine in the message body)


Solution

  • Through more searching, I found out two things:

    • ANSI_X3.4-1968 is apparently the canonical name for ASCII, which of course cannot encode ä. Also, =3F decodes as ? (don't know why it needs encoding in the first place).
    • There is a constructor InternetAddress(mail, name, charset)

    So, I'm now creating the InternetAddress with UTF-8, which fixes the problem.