Reusable
Reusable

Reputation: 1948

java Mail API receiving email attachment with vietnamese characters

Recently i have follow the Sun online guide on Java Mail API to send email, download email and download attachment. When comes to extracting attachment from an email, my code looks very similar to this post Download attachments using Java Mail

However, i encounter a problem when the user send the email attachment file name with vietnamese character, something like this: hệ thống thông.xls

When the code tries to get the filename with: bodyPart.getFileName() , the return file name is like this: h? th?ng =?iso-8859-1?b?dGj0bmcueGxz?=

I have try the following but without any luck

1) set Java VM parameter: -Dfile.encoding=UTF-8

2) try to look further into the Java Mail API on UTF-8 Character support, but cant find any

Any hint will help. Thank you!

Upvotes: 0

Views: 2412

Answers (2)

Abhinav Maheshwari
Abhinav Maheshwari

Reputation: 192

You can use MimeUtility.decodeWord(bodyPart.getFileName()) to get the actual name of the file.

Since mail headers can only contain ASCII characters, RFC 2047 describes how to encode other character sets and this method decodes the name as specified in RFC 2047.

Upvotes: 4

Mike Baranczak
Mike Baranczak

Reputation: 8374

ISO-8859-1 is the Latin-1 character set. It only includes Western European characters. So that might be a hint. Maybe the problem is with the client that's sending the attachment? Take a look at the raw source for the message, check if any of the headers say anything about the text encoding.

Upvotes: 1

Related Questions