Torque
Torque

Reputation: 43

Email with japanese characters in java

I am using java to send emails. The message sent can be in different languages, but when I send it in Japanese, the received mail just have "???????" characters.

The code is something like this:

 String content = "\u30d5\u30a1\u30a4\u30eb\u540d:";    
SMTPMessage message = new SMTPMessage(sess);
            message.setFrom(new InternetAddress(from));
            InternetAddress dests[] = new InternetAddress[]{ new InternetAddress(to) };
            message.setRecipients(Message.RecipientType.TO, dests);
            message.setSubject(subject);
            message.setContent(content, "text/html");

            Transport.send(message);

The content sent are unicode characters. How should I encode correctly the message?

Upvotes: 2

Views: 4700

Answers (4)

Bladean Mericle
Bladean Mericle

Reputation: 532

You tried "ISO-2022-JP" encoding?

message.setContent(content, "text/html; charset=ISO-2022-JP");
message.setHeader("Content-Type", "text/plain; charset=ISO-2022-JP");

Upvotes: 0

Alexander Pogrebnyak
Alexander Pogrebnyak

Reputation: 45576

You should use setText instead of setContent

message.setText( content, "UTF-8", "html" );

Upvotes: 2

JAB
JAB

Reputation: 21089

Instead of your use of setContent(), try:

message.setText(content, "UTF-8", "html");

http://javamail.kenai.com/nonav/javadocs/javax/mail/internet/MimeMessage.html#setText%28java.lang.String,%20java.lang.String,%20java.lang.String%29

Upvotes: 1

CoolBeans
CoolBeans

Reputation: 20800

Add this:- message.setHeader("Content-Type", "text/plain; charset=UTF-8");

Upvotes: 1

Related Questions