Bart van Heukelom
Bart van Heukelom

Reputation: 44114

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)

Upvotes: 3

Views: 1480

Answers (2)

Erik Kaju
Erik Kaju

Reputation: 3173

Good to see that defining the charset for InternetAddress object fixed it for you.

Another solution (especially if you do not have possibility to change the code) would be to run JVM with defined encoding via corresponding VM argument:

-Dfile.encoding=utf-8

Upvotes: 0

Bart van Heukelom
Bart van Heukelom

Reputation: 44114

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.

Upvotes: 2

Related Questions