如何使用JavaMail从Google Mail(Gmail)中读取包含多个标签的电子邮件?

在Google Mail中,我想获取已分配多个标签的邮件.例如,如果在收件箱中我们有三封电子邮件:

带有Label_A和Label_B的Email_1

带有Label_A和Label_B的Email_2

带有Label_A和Label_C的Email_3

然后我想同时选择Label_A和Label_B,即Email_1和Email_2.目前,以下代码适用于单标签情况,但有多种方法可以使用多个标签吗?谢谢.

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);
最佳答案
你应该可以做这样的事情:

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

转载注明原文:如何使用JavaMail从Google Mail(Gmail)中读取包含多个标签的电子邮件? - 代码日志