13

I am trying to send mails that may contain UTF-8 characters in subject, message body and in the attachment file name.

I am able to send UTF-8 characters as a part of Subject as well as Mesage body. However when I am sending an attachment having UTF-8 characters as a attachment file name, it is not displaying it correctly.

So my question is how can I set attachement filename as UTF-8?

Here is part of my code:

MimeBodyPart pdfPart = new MimeBodyPart();
pdfPart.setDataHandler(new DataHandler(ds));
pdfPart.setFileName(filename);
mimeMultipart.addBodyPart(pdfPart);

Later edit:

I replaced

pdfPart.setFileName(filename);

with

pdfPart.setFileName(MimeUtility.encodeText(filename, "UTF-8", null));

and it is working perfectly. Thanks all.

Tonia
  • 131
  • 1
  • 5

2 Answers2

0

MIME Headers (like Subject or Content-Disposition) must be mime-encoded, if they contain non-ascii chars.

Encoding is either "quoted printable" or "base64". I recommend for quoted-printable.

See here: Java: Encode String in quoted-printable

Community
  • 1
  • 1
Axel Amthor
  • 10,980
  • 1
  • 25
  • 44
0

I don't know how you send attachments. If upload through tomcat server, It could cause by value of URIEncoding in conf/server.xml

Joker
  • 1