Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Can't get the attachment filename out of a Part (with non ascii characters)

828394Jan 4 2011 — edited Jan 5 2011
Hello, 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
This post has been answered by Bill Shannon-Oracle on Jan 4 2011
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 2 2011
Added on Jan 4 2011
2 comments
1,139 views