views:

1240

answers:

4

I have problem of showing Turkish characters in mail sent with Java code. The characters are shown as question marks (?) in mail.

Message msg = new MimeMessage(mailSession);
msg.setHeader("Content-Encoding","ISO-8859-9");
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject,"iso-8859-9");
msg.setSentDate(new Date());
msg.setContent(messageText, "text/html;ISO-8859-9");
+1  A: 

It looks like ISO-8859-9 should be able to handle your Turkish letters alright. Is it possible that the text is decoded somewhere else with the wrong character encoding? For example, if the body of the email contains text from a web request, another email, or a file, perhaps the wrong decoder is specified at that point.

One way to check would be to print the numeric value of the Unicode code points in the String:

for (int idx = 0; idx < str.length(); ++idx) {
  System.out.println(Integer.toHexString(str.charAt(idx)));
}

If you see fffd, that is the "replacement character" and means that the decoding used when the String was created could not map a byte (or byte sequence) to a character. If you see 3f, that is a '?' character, and means that the text was corrupted even farther back.

erickson
A: 

? means that a character used in the mail is not representable as ISO-8859-9.

Stop using the legacy ISO-9957-x encodings and sent your mails as UTF-8 instead. This would solve your problem and future-proof your mailing code.

(Also think of a Turkish user wanting to sent an email mixing Turkish with, say, German or Ancient Greek characters, both not present in ISO-8859-9.

(Java strings are already unicode (though, not utf-8), so you won't have any problems coverting to utf-8).

foljs
I also try to send mail with UTF-8 encoding but same error occur. Is there an other way to set encoding instead of this code ( "msg.setContent(messageText, "text/html;ISO-8859-9");")
You have to also be sure that the text you read from your input sources (a file or web user input, etc) is read into Java strings with the proper encoding, and i.e not treat 8859-9 text as utf-8 or 8859-1.
foljs
A: 

You can find the solution here:

http://www.serkankonakci.com/?p=309

even with this code, you can send with attachment or single mail in Turkish. And you can find other Turkish charset problem, just search there.

A: 

Use UTF-8, here's a good overview of encoding: http://www.joelonsoftware.com/articles/Unicode.html

Chris Tek