Email with japanese characters in java

2020-07-26 14:06发布

问题:

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?

回答1:

You should use setText instead of setContent

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


回答2:

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



回答3:

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



回答4:

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");