如何使用JavaMail获取邮件帐户中可用文件夹的列表 [英] How to get the list of available folders in a mail account using JavaMail
问题描述
我使用JavaMail API连接到我的个人帐户。我有我创建的Gmail帐户中的文件夹(标签)列表+收件箱,草稿等默认文件夹。如何列出所有可用文件夹(默认和用户创建的)?
我可以使用此API访问特定文件夹: Folder inbox = store.getFolder(Inbox);
。是否有任何其他API可以获取邮件帐户中可用的文件夹列表?
以下是可用的代码。这会让你处理所有的标签。要深入文件夹
,您可以执行 folder.list()
,或者您可以使用 store.getDefaultFolder()。list(*)
来检索所有的文件夹和子文件夹,如其他答案中所建议的。
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
>>个人
单元
>>收据
¥b $ b>> Travel
>>工作
>> [Gmail]
OLD ANSWER
这些应该是:
http://java.sun.com/products/javamail/javadocs /javax/mail/Store.html#getSharedNamespaces%28%29
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)?
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?
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());
Output:
>> INBOX
>> Personal
>> Receipts
>> Travel
>> Work
>> [Gmail]
OLD ANSWER
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
这篇关于如何使用JavaMail获取邮件帐户中可用文件夹的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!