public static void folderlist() throws Exception {
Properties props = System.getProperties();
Session session = Session.getInstance(props, null);
Store store = session.getStore( new URLName( "imap://test:
[email protected]"));
store.connect();
Folder rf = store.getDefaultFolder();
dumpFolder(rf, false, "");
if ((rf.getType() & Folder.HOLDS_FOLDERS) != 0) {
Folder[] f = rf.list("%");
for (int i = 0; i < f.length; i++)
dumpFolder(f[i], true, " ");
}
store.close();
}
static void dumpFolder(Folder folder, boolean recurse, String tab) throws Exception {
System.out.println(tab + "Name: " + folder.getName());
System.out.println(tab + "Full Name: " + folder.getFullName());
System.out.println(tab + "URL: " + folder.getURLName());
if (!folder.isSubscribed())
System.out.println(tab + "Not Subscribed");
if ((folder.getType() & Folder.HOLDS_MESSAGES) != 0) {
if (folder.hasNewMessages())
System.out.println(tab + "Has New Messages");
System.out.println(tab + "Total Messages: " + folder.getMessageCount());
System.out.println(tab + "New Messages: " + folder.getNewMessageCount());
System.out.println(tab + "Unread Messages: " + folder.getUnreadMessageCount());
}
if ((folder.getType() & Folder.HOLDS_FOLDERS) != 0)
System.out.println(tab + "Is Directory");
/*
* Demonstrate use of IMAP folder attributes returned by the IMAP LIST
* response.
*/
if (folder instanceof IMAPFolder) {
IMAPFolder f = (IMAPFolder) folder;
String[] attrs = f.getAttributes();
if (attrs != null && attrs.length > 0) {
System.out.println(tab + "IMAP Attributes:");
for (int i = 0; i < attrs.length; i++)
System.out.println(tab + " " + attrs[i]);
}
}
System.out.println();
if ((folder.getType() & Folder.HOLDS_FOLDERS) != 0) {
if (recurse) {
Folder[] f = folder.list();
for (int i = 0; i < f.length; i++)
dumpFolder(f[i], recurse, tab + " ");
}
}
}