简体   繁体   English

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

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

In Google Mail, I would like to get messages that has been assigned multiple labels. 在Google Mail中,我想获取已分配多个标签的邮件。 For example, if in the Inbox we have three emails: 例如,如果在收件箱中我们有三封电子邮件:

Email_1 with Label_A and Label_B 带有Label_A和Label_B的Email_1

Email_2 with Label_A and Label_B 带有Label_A和Label_B的Email_2

Email_3 with Label_A and Label_C 带有Label_A和Label_C的Email_3

then I want to select those with Label_A and Label_B at the same time, which are Email_1 and Email_2. 然后我想同时选择Label_A和Label_B,即Email_1和Email_2。 Currently the following codes work for one-label situation, but is there any way to do it with more than one label? 目前,以下代码适用于单标签情况,但有多种方法可以使用多个标签吗? 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);

You should be able to 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"); 

I ended up having to write my own raw IMAP commands to allow javamail to use Gmail's IMAP extensions. 我最终必须编写自己的原始IMAP命令,以允许javamail使用Gmail的IMAP扩展。 Then this works. 然后这个工作。

Access to Gmail labels: X-GM-LABELS 访问Gmail标签:X-GM-LABELS

You then issue: folder.doCommand() with your command. 然后使用您的命令发出:folder.doCommand()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM