Javamail: can FetchProfile be used when searching

2019-05-03 12:11发布

问题:

According to http://java.sun.com/products/javamail/javadocs/javax/mail/FetchProfile.html,

Message[] msgs = folder.getMessages();
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
folder.fetch(msgs, fp);

However, if I want to get only the read messages by using search(), I don't have a means to specify a FetchProfile (as search() doesn't take such a parameter).

Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_WRITE);    
FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
Message messages[] = inbox.search(ft); //how to specify a FetchProfile here ??

Is there another way?

Thanks a lot in advance.

回答1:

I don't believe that you can.

In this, JavaMail mirrors the underlying IMAP commands -- first you figure out the set of messages that you're interested in, then you fetch data on those messages via a separate command. There's no way to coerce an IMAP server to return you FetchProfile-style data from a SEARCH command, and JavaMail is just conforming to IMAP's constraints on what gets returned when.