Java Mail API - Encoding problems

2019-02-06 10:24发布

I'm using Java Mail API and I'm trying to send an email through Gmail's SMTP. How my program works: java.util.Scanner class is used to get user input - I'm asking user for various parameters to be used in mail sending class; which does the following:

Message mailMessage = new MimeMessage(session);
mailMessage.setFrom(new InternetAddress("example@example.com"));
mailMessage.setRecipients(Message.RecipientType.TO,InternetAddress.parse(mail.getTo()));
mailMessage.setSubject(mail.getSubject());
mailMessage.setText(mail.getMessage());
Transport.send(mailMessage);

Everything works as long as I use ASCII symbols/ chars. But whenever I want to use "country-specific" characters - like [õäöü] - I get bunch of weird-looking symbols...

Techniques I've used so far(which don't work for me):

setHeader("Content-Type", "text/plain; charset=UTF-8");
setHeader("Content-Encoding","ISO-8859-9");
setContent(message, "text/plain; charset=iso-8859-2");

Note: everything is displayed correctly inside an IDE when System.out.println() is performed to display the message to be sent.

EDIT: e.x. when sent message body is [õäöü] It's displayed [ä„”?] in Gmail.

EDIT: When mailMessage.setText(MimeUtility.encodeText(mail.getMessage(), "UTF-8", "Q")); is used, then the output in Gmail is following:

"=?UTF-8?Q?=C3=A4=E2=80=9E=E2=80=9D=EF=BF=BD;=0D=0A?="

ANOTHER EDIT: Interestingly, when I do: mailMessage.setText(strVar + "õäöü", "ISO-8859-1"); It actually appends "õäöü" nicely in my email (but the first part[strVar] of the string is still full of ?'s and []'s).

8条回答
啃猪蹄的小仙女
2楼-- · 2019-02-06 10:52
    MimeMessage message = new MimeMessage(session);
    message.setSubject(subject, "UTF-8");
    message.setText(body, "UTF-8");

So one has to set the character encoding for both, body and subject.


Addendum because of comment of @bartac

For the corresponding MimeBodyPart do a setHeader("Content-Type", "text/plain; charset=UTF-8").

查看更多
我命由我不由天
3楼-- · 2019-02-06 10:53
JavaMailSenderImpl emailSender = new JavaMailSenderImpl();
mailSender.setHost("...");

MimeMessage message = emailSender.createMimeMessage();
message.setSubject("...", "UTF-8");
message.setText("...", "UTF-8");

MimeMessageHelper helper = new MimeMessageHelper(message, "UTF-8");
helper.setFrom(from);
helper.setTo(to);

emailSender.send(message);
查看更多
▲ chillily
4楼-- · 2019-02-06 11:03

In case if you use HTML messages, try this:

Message message = new MimeMessage(session);
message.setContent(htmlText, "text/html; charset=UTF-8");
查看更多
戒情不戒烟
5楼-- · 2019-02-06 11:07

1- Consider you want to send an email with this string in the body:

"Olá João!"

2 - As the code is running in the GAE server, this string is interpreted with the default ASCII encoding. To send this email with the correct accented characters, define the String as:

String body = "Ol\u00e1 Jo\u00e3o!";

The special characters are manually defined with its UTF-8 codes. Search the codes you need in the table http://www.utf8-chartable.de/

3- Convert the string encoding to UTF-8. All the codes manually typed will be now correctly interpreted:

Session session = Session.getDefaultInstance(props);
MimeMessage message = new MimeMessage(session);
String encodedSubject = new String (subject.getBytes("UTF-8"),"UTF-8");
String encodedBody = new String (body.getBytes("UTF-8"),"UTF-8");
message.setSubject(encodedSubject, "UTF-8");
message.setText(encodedBody, "UTF-8");
查看更多
Emotional °昔
6楼-- · 2019-02-06 11:12

You should use setText(String text, String charset) or setText(String text, String charset, String subtype) to set the text body with a specific encoding.

MimeUtility.encodeText() is not meant for body text, but only for encoded text in headers (and then only for headers set with setHeader or addHeader).

查看更多
萌系小妹纸
7楼-- · 2019-02-06 11:16

Use the encodeText method from MimeUtility:

MimeUtility.encodeText(mail_subject, "UTF-8", "B")

(I don't know what "B" means, keep calm, and just use. Actually it can be "B" or "Q".)

查看更多
登录 后发表回答