0

I'm using JavaMail to fetch data of messages from an IMAP account. For example, I use the following command to retrieve the bodies of the messages with uid 1 to 4:

UID FETCH 1:4 BODY[]

Is there a way to fetch specific body parts for specific messages? For example, I would like to use something like:

UID FETCH (1 BODY[1.3]) (2 BODY[1.1]) (3 BODY[2.1]) (4 BODY[1.4])

PS: I know I could also send 4 distinct commands, but for performance reasons I can't afford to make a separate request for each message.

xpages-noob
  • 1,569
  • 1
  • 10
  • 37

1 Answers1

2

You have to retrieve the same bodyparts for each message. That is, if you want part 1 for messages 2 and 4 and part 2 for message 3, you cannot send fewer than two commands:

a uid fetch 2,4 (body[1])
b uid fetch 3 (body[3])

The good news is that you can send them at the same time. So long as the commands don't conflict, you can send the second without waiting for the result of the first. Provided you can find a way to do that with Javamail.

(Or perhaps body.peek[1], which is the same as body[] except that it doesn't set the \seen flag.)

arnt
  • 8,949
  • 5
  • 24
  • 32
  • Thanks for your answer. Seems as if you are responsible for all fetching-related questions on stackoverflow :) . "Unfortunately" you confirmed my expectation that it does not work as I would have needed it to. I guess I'll have to use multithreading to send the commands parallely. – xpages-noob Jan 29 '15 at 13:18
  • 1
    Post such questions a few hours later and [Max](http://stackoverflow.com/users/1442918/max) answers instead of me ;) Using threads is one possibility, sending several concurrent commands on one connection is the other. – arnt Jan 29 '15 at 14:36
  • 2
    JavaMail doesn't support the sort of low level IMAP commands you're looking for. Multiple threads won't help because JavaMail will single thread all access to the protocol object that sends the commands and waits for the responses. You could consider using multiple connections, but that has its own set of problems. You might be able to extend the JavaMail source code to support what you need. Can you provide more background on what you're trying to accomplish at a higher level and what performance analysis you've done already? – Bill Shannon Jan 29 '15 at 22:07
  • @BillShannon: What I actually want to do is to get some preview text for a bunch of messages, without loading attachments (http://stackoverflow.com/q/28166182/1697566) . Therefore I fetch the body structure first and search for a "suitable" body part in each message. Afterwards I want to fetch the determined body sections for all messages with 1 request. At the moment, I'm using a custom protocol command, as described here http://stackoverflow.com/a/9620801/1697566, that fetches "BODY.PEEK[1]<0.1024>". however, that first section could be anything... – xpages-noob Jan 30 '15 at 08:02
  • 1
    I've done that and it's not too bad. There aren't many different part numbers, you see, so even if you must retrieve the right part for a hundred messages you can still do it with three commands: Thirty or so need part 1, another thirty need part 1.2 and most of the rest need 2. The number of commands needed scales sublinearly. (What annoys me is that I get the responses in almost exactly the opposite order of what I want.) – arnt Jan 30 '15 at 08:11
  • I guess I won't get around sending multiple commands, no matter how hard I try :) . When I'm done with it, I'll post the Java code in the original question (http://stackoverflow.com/q/28166182/1697566). Thanks very very much for all your help on this topic. – xpages-noob Jan 30 '15 at 08:39
  • 2
    It sounds like you've pretty much got this figured out, but I have to ask the obvious question... Certainly executing one command will have better performance than executing multiple commands, but have you determined that the difference actually matters to your application? Can the user actually see the difference? Are you really displaying the preview text for 100 messages at once? Premature optimization and all that.... – Bill Shannon Jan 31 '15 at 03:46
  • @BillShannon: The questions you ask are actually the only ones that should have mattered to me, but I was too focused on getting the best performance and spent far too much time on this issue. After some benchmarking "it turned out" that the temporal overhead of sending 2 or 3 additional commands is negligible to the time it takes to actually load the data. – xpages-noob Feb 04 '15 at 10:54
  • 1
    And we all re-learn the lesson about [premature optimization](http://c2.com/cgi/wiki?PrematureOptimization)! Thanks for the follow up! – Bill Shannon Feb 05 '15 at 02:21