Read Recent and Unseen message using javax.mail

2019-09-07 07:43发布

问题:

I am using javax.mail to read messages from inbox folder using 'imaps' protocol. I am using the below code snippet:

  Session session = Session.getDefaultInstance(props, null);
  Store store = session.getStore("imaps");
  store.connect();
  store.getFolder("inbox");
  inbox.open(Folder.READ_WRITE);
  FlagTerm unseenFlagTerm = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
  FlagTerm recentFlagTerm = new FlagTerm(new Flags(Flags.Flag.RECENT), true);

But I am not getting any messages. I want the most recent message which is also still not read/seen. Please propose any better solution? I am still not sure what 'new Flags(Flags.Flag.RECENT) set TRUE or FALSE' do?

回答1:

You haven't included all your code. Presumably you're using those two FlagTerms in an AndTerm, which you pass to the search method.

It's somewhat server-dependent when the RECENT flag is set on a message, and when it's cleared. If you open a folder and close it, it may clear all the RECENT flags, on the assumption that you've seen which messages are recent.

You might have better luck just ignoring the RECENT flag and only looking for the SEEN flag not being set.

You also need to decide whether "most recent" means "most recently received" or "most recently sent". The former is easy; it's just the last message in the returned array. The latter will require you to sort the returned messages by sent date.



标签: java javamail