How to use JavaMail to read emails with multiple labels from Google Mail (Gmail)?

In Google Mail, I would like to receive messages that have been assigned several labels. For example, if there are three letters in your inbox:

Email_1 with Label_A and Label_B

Email_2 with Label_A and Label_B

Email_3 with Label_A and Label_C

then I want to simultaneously select those that have Label_A and Label_B, which are Email_1 and Email_2. The following codes currently work for a single label situation, but is there a way to do this with multiple labels? Thanks.


Properties props = System.getProperties(); Session session = Session.getInstance(props, null); Store store = session.getStore("imaps"); store.connect("imap.gmail.com", -1, " abc@def.com ", "password"); Folder folder = store.getDefaultFolder(); folder = folder.getFolder("Label_A"); folder.open(Folder.READ_WRITE); int totalMessages = folder.getMessageCount(); int newMessages = folder.getNewMessageCount(); System.out.println("Total messages = " + totalMessages); System.out.println("New messages = " + newMessages); 

+4
source share
2 answers

You should do something like this:

 private Store store; private Folder Label_A; private Folder Label_B; ... Label_A = store.getFolder("Label_A"); Label_B = store.getFolder("Label_B"); 
+2
source

I had to write my own IMAP commands to allow javamail to use the Gmail email extensions. Then it works.

Gmail Tag Access: X-GM-LABELS

Then you issue: folder.doCommand () using your command.

+1
source

All Articles