Decode Mail.app Email Attachment File Name in Java

I am having trouble decrypting the email attachment file name. I am currently using JavaMail 1.4.2. The file is called "Żółw.rtf" (this is polishing for Turtle.rtf). Mail is sent using Mail.app (which seems very significant). Important headings are:

--Apple-Mail-19-721116558 Content-Disposition: attachment; filename*=utf-8''Z%CC%87o%CC%81%C5%82w.rtf Content-Type: text/rtf; x-unix-mode=0644; name="=?utf-8?Q?Z=CC=87o=CC=81=C5=82w=2Ertf?=" Content-Transfer-Encoding: 7bit 

The corresponding javax.mail.Part.getFileName () returns "=? Utf-8? Q? Z = CC = 87o = CC = 81 = C5 = 82w = 2Ertf? =", Which after applying MimeUtility.decodeText is: "ZÃÃÃÃÃÃÅÅÅÇÇ .rtf ". Clearly not the original :).

For comparison, MimeUtility.encodeText returns:

 =?UTF-8?Q?=C5=BB=C3=B3=C5=82w.rtf?= 

Unlike:

 =?utf-8?Q?Z=CC=87o=CC=81=C5=82w=2Ertf?= 

outgoing from email.

According to my research, the letter “Ż” can be encoded in two ways: either as a single letter, or as “Z” + above a dot. MimeUtility.encodeText uses the first, Mail.app last.

However, I want to be able to decode both. Is there a way to decode the file name when sending from Mail.app using JavaMail? Or maybe there is another library?

Thanks! Adam

+7
source share
2 answers

Turns out you should normalize the line:

 String decoded = MimeUtility.decodeText(part.getFileName()); return Normalizer.normalize(decoded, Normalizer.Form.NFC); 

Strange, but it works! :) In more detail, since Mail.app encodes "Ż" as two characters: "Z" + "dot-above", this must then be combined using the Normalizer.

Adam

+11
source

I don’t know if it’s useful that I have a part of java code that checks attachments of mail files, and if present, save it in the specified path to the file with the specified name and extension, and if the file name already exists in the path , then it increases the value to the end of the file name. So here is the code snippet:

 enter 

Multi-page messages mp = (multi-page) [i] .getContent ();

  for (int j=0, n=mp.getCount(); j<n; j++) { Part part = mp.getBodyPart(j); String disposition = part.getDisposition(); if ((disposition != null) && ((disposition.equals(Part.ATTACHMENT) || (disposition.equals(Part.INLINE))))){ String path = "c:\\Temp; saveFile(part.getFileName(), part.getInputStream(),path); } } public static void saveFile(String filename,InputStream input, String path) throws IOException { if (filename == null) { filename = File.createTempFile("xx", ".out").getName(); } try{ boolean success = (new File(path)).mkdirs(); if (success) { System.out.println("Directories: " + path + " created"); } }catch (Exception e){//Catch exception if any System.err.println("Error: " + e.getMessage()); } String filenamepath = path + "//"+filename; File file = new File(filenamepath); for (int i=0; file.exists(); i++) { String fname=""; String ext=""; int mid= filenamepath.lastIndexOf("."); fname=filenamepath.substring(0,mid); ext=filenamepath.substring(mid+1,filenamepath.length()); file = new File(newpath); } FileOutputStream fos = new FileOutputStream(file); BufferedOutputStream bos = new BufferedOutputStream(fos); BufferedInputStream bis = new BufferedInputStream(input); int aByte; while ((aByte = bis.read()) != -1) { bos.write(aByte); } bos.flush(); bos.close(); bis.close(); System.out.println("File saved to :"+file+"*******"); } 

here

I hope you find this helpful.

Regards, Rajiv

0
source

All Articles