JavaMail - attachment file name does not display UTF-8 characters correctly

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

I can send UTF-8 characters as part of the Subject as well as the body of the Mesage. However, when I send an attachment that has UTF-8 characters as the name of the attachment file, it does not display it correctly.

So my question is, how can I set the attachment file name as UTF-8?

Here is part of my code:

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

Next edit:

I replaced

 pdfPart.setFileName(filename); 

from

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

and it works great. Thanks to everyone.

+5
source share
2 answers

MIME headers (such as Subject or Content-Disposition ) must be encoded in MIME if they contain non-ascii characters.

The encoding is " quoted printable " or " base64 ". I recommend for quoted-printable .

See here: Java: encode string in quotation marks-printed

0
source

I do not know how you send attachments. If you download via tomcat server, it can call the URIEncoding value in conf / server.xml

0
source

Source: https://habr.com/ru/post/1213125/


All Articles