Can't get the attachment filename out of a Part (with non ascii characters)
828394Jan 4 2011 — edited Jan 5 2011Hello, all and happy new year :)
My issue is with non ascii filename in attachments... Yes i've read the FAQ : http://www.oracle.com/technetwork/java/faq-135477.html#encodefilename
I can't get the filename out of the BodyPart for those kind of attachments
here's my unit test :
/**
* contains various parts from various mailer encoded in different ways...
*
*/
private enum EncodedFileNamePart{
OUTLOOK("Content-Type: text/plain;\n name=\"=?iso-8859-1?Q?c'estd=E9j=E0no=EBl=E7ac'estcool.txt?=\" \nContent-Transfer-Encoding: 7bit\nContent-Disposition: attachment;\n filename=\"=?iso-8859-1?Q?c'estd=E9j=E0no=EBl=E7ac'estcool.txt?=\" \n\nnoel 2010\n","c'estdéjànoëlçac'estcool.txt"),
GMAIL("Content-Type: text/plain; charset=US-ASCII; name=\"=?ISO-8859-1?B?ZOlq4G5v62znYWNlc3Rjb29sLnR4dA==?=\"\nContent-Disposition: attachment; filename=\"=?ISO-8859-1?B?ZOlq4G5v62znYWNlc3Rjb29sLnR4dA==?=\"\nContent-Transfer-Encoding: base64\nX-Attachment-Id: f_giityr5r0\n\namluZ2xlIGJlbGxzIQo=\n","déjànoëlçacestcool.txt"),
THUNDERBIRD("Content-Type: text/plain;\n name=\"=?ISO-8859-1?Q?d=E9j=E0no=EBl=E7acestcool=2Etxt?=\"\nContent-Transfer-Encoding: 7bit\nContent-Disposition: attachment;\n filename*0*=ISO-8859-1''%64%E9%6A%E0%6E%6F%EB%6C%E7%61%63%65%73%74%63%6F;\n filename*1*=%6F%6C%2E%74%78%74\n\njingle bells!\n","déjànoëlçacestcool.txt"),
EVOLUTION("Content-Disposition: attachment; filename*=ISO-8859-1''d%E9j%E0no%EBl.txt\nContent-Type: text/plain; name*=ISO-8859-1''d%E9j%E0no%EBl.txt; charset=\"UTF-8\" \nContent-Transfer-Encoding: 7bit\n\njingle bells\n","déjànoël.txt"),
;
String content=null;
String target=null;
private EncodedFileNamePart(String content,String target){
this.content=content;
this.target=target;
}
public Part get(){
try{
ByteArrayInputStream bis = new ByteArrayInputStream(this.content.getBytes());
Part part = new MimeBodyPart(bis);
bis.close();
return part;
}
catch(Throwable e){
return null;
}
}
public String getTarget(){
return this.target;
}
}
@Test
public void testJavamailDecode() throws MessagingException, UnsupportedEncodingException{
System.setProperty("mail.mime.encodefilename", "true");
System.setProperty("mail.mime.decodefilename", "true");
for(EncodedFileNamePart part : EncodedFileNamePart.values())
assertEquals(part.name(),MimeUtility.decodeText(part.get().getFileName()),part.getTarget());
}
I take a NullPointerExcepion in the decodeText because getFileName() return null for the EVOLUTION case, and work well with OUTLOOK, THUNDERBIRD and GMAIL.
Evolution's content type is "Content-Disposition: attachment; filename*=ISO-8859-1''d%E9j%E0no%EBl.txt" wich doesn't look like the other (looks like the RFC 2616 or RFC5987 to do it.)
How can i handle this situation except by writting my own decoder?
Thanks for your answers!
Edited by: user13619058 on 4 janv. 2011 07:44