Salvin Francis
Salvin Francis

Reputation: 4267

Need a way to search mail for a message id using imap

I am using Java Mail api to access a gmail account (not fixed). I need to search all mails for a given Mail id (). The search should terminate on the FIRST occurence of the message with given ID (Optimization : only a single mail should exist with the given id)

i would of course would like to skip the "All mail" folder.

The simplest method is to do :

  1. Get Default folder
  2. Get all folders in default.
    1. Search using MessageIDTerm in current folder
    2. if message was not found, repeat step2 recursively for sub folders
    3. else return found folder.

This causes a lot of wasted server calls that are unnecessary, does any one have a more optimal approach?

Upvotes: 0

Views: 2064

Answers (2)

Salvin Francis
Salvin Francis

Reputation: 4267

folder.list("*");

This is a method to return recursively all list of folders. So I would have to run just 1 loop for folders!! And of course one loop for every message found in the search.

Upvotes: 0

Stu Thompson
Stu Thompson

Reputation: 38868

It's been ages since I've messed with all this stuff, but you should be able to:

  • Use the IMAP4 UID command, which searches for a given id (or range of ids). Reference RFC3501, section 6.4.8
  • Use this with the doCommand() on the IMAPFolder object, and recurs through your folders as required.

Upvotes: 2

Related Questions