如何使用JavaMail从Google Mail(Gmail)中读取带有多个标签的电子邮件?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你应该能够做这样的事情:
You should be able to do something like this:
我最终不得不编写自己的原始 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.