List of usage examples for javax.mail Folder list
public Folder[] list() throws MessagingException
From source file:net.wastl.webmail.server.WebMailSession.java
/** * Construct the folder subtree for the given folder and append it to * xml_parent./*from w w w . ja va2 s . c o m*/ * * N.b. this method does not necessarily create a new XML Folder Element. * If called with subscribed_only and the target Folder (and in some * cases its descendants) are not subscribed, no Element will be created * and 0 will be returned. * <P> * Pop servers don't support nesting at all, so you'll just get a single * level out of this method. * <P> * There is a critical subscribed_only difference in behavior between * Maildir and mbox type mail servers. * Maildir folders are all HOLDS_MESSAGES, whether empty or not, and * these folders have a subscribed attribute which the user can set, * and which we honor. * mbox folders, on the other hand, have no subscribed attribute for * their !HOLDS_MESSAGE folders, so we must recurse to all of the * descendant HOLDS_MESSAGE folders to see if we should show. * * @param folder the folder where we begin * @param xml_parent XML Element where the gathered information will be * appended * @param subscribed_only Only add 'subscribed' folders * @param doCount Whether to generate message counts for Elements * corresponding to HOLDS_MESSAGE folders. * @returns maximum depth of the folder tree (needed to calculate the * necessary columns in a table). Returns 0 if no XML elements added. */ protected int getFolderTree(Folder folder, Element xml_parent, boolean subscribed_only, boolean doCount) { int generatedDepth = 0; int folderType; Element xml_folder; try { folderType = folder.getType(); } catch (MessagingException ex) { log.error("Can't get enough info from server to even make Gui node", ex); xml_parent.setAttribute("error", "For child '" + folder.getName() + ": " + ex.getMessage()); return 0; } boolean holds_folders = (folderType & Folder.HOLDS_FOLDERS) != 0; boolean holds_messages = (folderType & Folder.HOLDS_MESSAGES) != 0; // Sanity check: if ((!holds_folders) && !holds_messages) { log.fatal("Folder can hold neither folders nor messages: " + folder.getFullName()); throw new RuntimeException("Folder can hold neither folders nor messages: " + folder.getFullName()); } if (subscribed_only && holds_messages && !folder.isSubscribed()) return generatedDepth; // Return right away and save a LOT OF WORK // N.b. we honor folder.isSubscribed() only for holds_message // folders. That means all Maildir server folders, and all // mbox server folders except for mbox directories. In this // last case, we must recurse to determine whether to show folder. String id = generateFolderHash(folder); xml_folder = model.createFolder(id, folder.getName(), holds_folders, holds_messages); // XMLUserModel.createFolder() declares no throws. If any Exceptions // are expected from it, move the statement above into the try block. // The xml_folder Element here will be orphaned and GC'd if we don't // appendChild (in which case we return 0). if (doCount && holds_messages) try { // this folder will definitely be added! /* This folder can contain messages */ Element messagelist = model.createMessageList(); int total_messages = folder.getMessageCount(); int new_messages = folder.getNewMessageCount(); if (total_messages == -1 || new_messages == -1 || !folder.isOpen()) { folder.open(Folder.READ_ONLY); total_messages = folder.getMessageCount(); new_messages = folder.getNewMessageCount(); } if (folder.isOpen()) folder.close(false); messagelist.setAttribute("total", total_messages + ""); messagelist.setAttribute("new", new_messages + ""); log.debug("Counted " + new_messages + '/' + total_messages + " for folder " + folder.getFullName()); xml_folder.appendChild(messagelist); } catch (MessagingException ex) { log.warn("Failed to count messages in folder '" + folder.getFullName() + "'", ex); xml_folder.setAttribute("error", ex.getMessage()); } int descendantDepth = 0; if (holds_folders) try { Set<String> fullNameSet = new HashSet<String>(); /* Recursively add subfolders to the XML model */ // DO NOT USE listSubscribed(), because with !HOLDS_MESSAGE // folders, that skips non-Message Folders which may contain // subscribed descendants! for (Folder f : folder.list()) { if (!fullNameSet.add(f.getFullName())) { log.warn("Skipping duplicate subfolder returned by mail" + " server: " + f.getFullName()); continue; } if (subscribed_only && (f.getType() & Folder.HOLDS_MESSAGES) != 0 && !f.isSubscribed()) continue; /* If we recursed here, the getFolderTree() would * just return 0 and no harm done. * Just helping performance by preventing a recursion * here. * For comment on the logic here, see the same test * towards the top of this method (before recursion). */ int depth = getFolderTree(f, xml_folder, subscribed_only, doCount); if (depth > descendantDepth) descendantDepth = depth; } generatedDepth += descendantDepth; } catch (MessagingException ex) { xml_folder.setAttribute("error", ex.getMessage()); } // We've already validated that if subscribed_only and holds_message // then folder is subcribed. Also verified either holds_m or holds_f. // Only have to check the !holds_message case. if (subscribed_only && (!holds_messages) && descendantDepth < 1) { xml_folder = null; // Unnecessary, but may encourage GC return generatedDepth; } /* We ALWAYS return only subscribed folders except for these two * distinct cases: */ xml_folder.setAttribute("subscribed", ((holds_messages && !folder.isSubscribed()) || ((!holds_messages) && descendantDepth < 1)) ? "false" : "true"); // N.b. our Element's "subscribed" element does not correspond 1:1 // to Folder.isSubscribed(), since non-message-holding Folders have // no "subscribed" attribute. folders.put(id, folder); xml_parent.appendChild(xml_folder); generatedDepth++; // Add the count for xml_folder return generatedDepth; }
From source file:net.wastl.webmail.server.WebMailSession.java
/** * Subscribe all folders for a Mailhost/* ww w .j a va 2s.c o m*/ * Do it the non-recursive way: Uses a simple Queue :-) * * @deprecated Don't use this method, it touches every folder in the user's * mail directory, which in some cases may be the complete * home directory. */ @Deprecated public void setSubscribedAll(String id, boolean subscribed) throws MessagingException { Folder folder = getRootFolder(id); net.wastl.webmail.misc.Queue q = new net.wastl.webmail.misc.Queue(); q.queue(folder); // Only IMAP supports subscription... try { while (!q.isEmpty()) { folder = (Folder) q.next(); folder.setSubscribed(subscribed); Folder[] list = folder.list(); for (int i = 0; i < list.length; i++) { q.queue(list[i]); } } } catch (MessagingException ex) { } }
From source file:org.apache.hupa.server.handler.FetchFoldersHandler.java
/** * Walk through the folder's sub-folders and add sub-folders to current imapFolder * /*from w ww . j a v a 2 s .c o m*/ * @param folder Folder to walk * @param imapFolder Current IMAPFolder * @throws ActionException If an error occurs * @throws MessagingException If an error occurs */ private void walkFolders(Folder folder, IMAPFolder imapFolder) throws ActionException, MessagingException { for (Folder f : folder.list()) { IMAPFolder iFolder = createIMAPFolder(f); imapFolder.getChildIMAPFolders().add(iFolder); walkFolders(f, iFolder); } }
From source file:org.pentaho.di.job.entries.getpop.MailConnection.java
private HashSet<String> returnSubfolders(Folder folder) throws KettleException { HashSet<String> list = new HashSet<String>(); try {/*from w w w .j av a 2 s .c o m*/ if ((folder.getType() & Folder.HOLDS_FOLDERS) != 0) { Folder[] f = folder.list(); for (int i = 0; i < f.length; i++) { // Search for sub folders if ((f[i].getType() & Folder.HOLDS_FOLDERS) != 0) { list.add(f[i].getFullName()); list.addAll(returnSubfolders(f[i])); } } } } catch (MessagingException m) { throw new KettleException(m); } return list; }