Does JavaMail support Multipart nesting? When trying...
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Sean_Hignett
Posted On:   Thursday, November 2, 2000 01:43 PM

Does JavaMail support Multipart nesting? When trying to read a "System Administrator" rejection message (w/attachment) from Exchange, the original getContent() returns a Multipart. However in the original message (2nd Part of Multipart) the attachment is lumped in with the Part (text/plain) of the original message. How do I access the attachment in this situation?

Re: Does JavaMail support Multipart nesting? When trying...

Posted By:   Dieter_Wimberger  
Posted On:   Sunday, August 12, 2001 08:48 AM

It is possible to access all the parts of a message, even if they are nested multiparts.
One possible approach is to work recursively and "flatten" out the parts into one straight list (this is the one I prefer, even if it does not reflect exactly how the message looks like "architecturally").

You start from:


[...]
if(msg.isMimeType("multipart/*")) {
//get main body part
Multipart mp=(Multipart)msg.getContent();

//build flatlist
List partlist=new ArrayList(10); //example, use your own size idea
buildPartInfoList(partlist,mp);
}
[...]


And the recursive method is:


[...]
private void buildPartInfoList(List partlist, Multipart mp)
throws Exception {

for (int i=0; i //Get part
Part apart=mp.getBodyPart(i);
//handle single & multiparts
if(apart.isMimeType("multipart/*")) {
//recurse
buildPartInfoList(partlist,(Multipart)apart.getContent());
} else {
//append the part
partlist.add(apart);
}
}
}//buildPartList
[...]


The list partlist will afterwards contain all the parts that have been encountered with the multipart message.


If you do not like this approach, you can easily extract the idea of how to retrieve a specific nested part from the code above.

About | Sitemap | Contact