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

发布于 2024-09-15 01:48:55 字数 945 浏览 9 评论 0原文

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

Email_1 带有 Label_A 和 Label_B

Email_2 带有 Label_A 和 Label_B

Email_3 带有 Label_A 和 Label_C

那么我想同时选择那些带有 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, "[email protected]", "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);

In Google Mail, I would like to get messages that has been assigned multiple labels. For example, if in the Inbox we have three emails:

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 select those with Label_A and Label_B at the same time, which are Email_1 and 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, "[email protected]", "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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

所有深爱都是秘密 2024-09-22 01:48:55

你应该能够做这样的事情:

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

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"); 
演出会有结束 2024-09-22 01:48:55

我最终不得不编写自己的原始 IMAP 命令来允许 javamail 使用 Gmail 的 IMAP 扩展。然后这行得通。

访问Gmail 标签:X-GM-LABELS

然后,您可以使用您的命令发出:folder.doCommand()。

I ended up having to write my own raw IMAP commands to allow javamail to use Gmail's IMAP extensions. Then this works.

Access to Gmail labels: X-GM-LABELS

You then issue: folder.doCommand() with your command.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文