How to get the list of available folders in a mail account using JavaMail(如何使用 JavaMail 获取邮件帐户中可用文件夹的列表)
问题描述
我正在使用 JavaMail API 连接到我的个人帐户.我的 Gmail 帐户中有我创建的文件夹(标签)列表 + 收件箱、草稿等默认文件夹.如何列出所有可用文件夹(默认文件夹和用户创建的文件夹)?
I am using JavaMail API to connect to my personal account. I have list of folders (labels) in my Gmail account which I created + the default folders like Inbox, Drafts etc. How can I list all the available folders (the default and the user created)?
我可以使用以下 API 访问特定文件夹:Folder inbox = store.getFolder("Inbox");
.是否有任何其他 API 可以获取邮件帐户中可用的文件夹列表?
I can access the particular folder using this API: Folder inbox = store.getFolder("Inbox");
. Is there any other API to get the list of folders available in a mail account?
推荐答案
这是有效的代码.这将使您能够处理所有标签.要深入folder
,您可以执行folder.list()
或使用store.getDefaultFolder().list("*")
以检索其他答案中建议的所有文件夹和子文件夹.
Here is the code that works. This will give you handle to all the Labels. To go deeper in a folder
, you may perform folder.list()
or you can use store.getDefaultFolder().list("*")
to retrieve all the folders and sub-folders as suggested in the other answer.
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com", "YOURMAILID@gmail.com", "UR_P@ZZWRD");
System.out.println(store);
Folder[] f = store.getDefaultFolder().list();
for(Folder fd:f)
System.out.println(">> "+fd.getName());
输出:
>>收件箱
>>个人
>>收据
>>旅游
>>工作
>>[邮箱]
>> INBOX
>> Personal
>> Receipts
>> Travel
>> Work
>> [Gmail]
<小时>
老答案
请注意这是不正确的,它在 this answer by dkarp
Please note this is not correct, it's rightly pointed in this answer by dkarp
这些应该可以:
These should do:
http://java.sun.com/products/javamail/javadocs/javax/mail/Store.html#getSharedNamespaces%28%29
http://java.sun.com/products/javamail/javadocs/javax/mail/Store.html#getUserNamespaces%28java.lang.String%29
这篇关于如何使用 JavaMail 获取邮件帐户中可用文件夹的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 JavaMail 获取邮件帐户中可用文件夹的列表


- 获取数字的最后一位 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 转换 ldap 日期 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01