I\'m using JavaMail 1.5.2 to read messages from IMAP accounts. To reduce the number of requests to the host I prefetch some message data, like From, Date, Message-ID etc.:
You have to retrieve the bodystructure first, then loop across the message structure, check the mime type of each part, and download the parts you want. IMAP lets you download all of the parts using one command, so if Javamail is a little smart, you should be able to do this with two IMAP commands, no matter how many bodyparts you end up wanting to download.
The IMAP commands, if you're the type who likes to look at wire traffic, should be something like a uid fetch 234789 bodystructure
followed by b uid fetch 234789 (body.peek[1.1] body.peek[2])
.