I\'ve a problem of downloading arabic attachment files using java mail.
The file name is always ambiguous.
The problem is the Bodypart retrieve
This part seems broken to me:
byte[] fileBytes = bodyPart.getFileName().toString().getBytes();
String filename = new String(fileBytes, "UTF-8");
You're using the default platform encoding to encode the filename (which you'd already got as a string) as bytes, and then decoding them using UTF-8.
What's wrong with:
String filename = bodyPart.getFileName();
? Don't do any more encoding and decoding than you need. Of course, whether your file system can handle such a filename is a different matter, but at least you're not losing data at this point any more...
(Your method of copying the file data is also nastily inefficient and will leave open streams in the case of exceptions. You might want to look at Guava to write easy code which gets this write...)