java编写一个邮件程序显示收件箱中所有的邮件列表,如何使用JavaMail获取邮件帐户中可用文件夹的列表...

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:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值