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");
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.
Use UTF-8, here's a good overview of encoding: http://www.joelonsoftware.com/articles/Unicode.html
You can find the solution here:
http://www.serkankonakci.com/Project/wordpress/2009/02/04/java-mail-ile-mail-gonderme-ornegi/
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.
来源:https://stackoverflow.com/questions/773264/java-mail-problem-with-turkish-characters