Within a Java project I am trying to retrieve the emails of a gmail account that are flagged with some specific gmail "labels" with this code :
//List of flags
Flags flagsProcessed = new Flags("AiProcessed");
Flags flagsFailedToProcess = new Flags("AiNotProcessed");
Flags flagsAi = new Flags("Ai");
// list of flags terms
FlagTerm unseenFlagTerm = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
FlagTerm aiTerm = new FlagTerm(flagsAi, true);
FlagTerm aiNotProcessed = new FlagTerm(flagsProcessed, false);
SearchTerm toBeProcessedByAiAndNotAlreadyProcessed =new AndTerm( aiTerm, aiNotProcessed);
Message[] messagesUnseen = inbox.search(unseenFlagTerm);
logger.info("unread : " + messagesUnseen.length);
Message[] messagesToProcess = inbox.search(toBeProcessedByAiAndNotAlreadyProcessed);
logger.info("toProcess : " + messagesToProcess.length);
Message[] messages = ArrayUtils.addAll(messagesUnseen, messagesToProcess);
logger.info("There are messages unread or with ai flags to process : " + messages.length);
but it does not work, even the messages tagged with the righ labels with the gmail web interface are not retrieved, it looks like the gmail labels are not really "flags" as per Javax.mail concept. I read that maybe i should manage the gmails labels as folders, but then how to search on multiple folders ?
I can't beleive that nobody ever tried to integrate a gmail inbox using labels into JAVA, anybody with an idea to put me on the right track ?
i think, in JavaMail api, Gmail labels are not directly mapped to the Flags. You have to use the Folder.search(your_search_term) method with a search_term that checks for the presence of a specific gmail label.
Please see the documentation. I am look forward on this issue. Happy Coding...!! :)