Example usage for javax.mail Folder list

List of usage examples for javax.mail Folder list

Introduction

In this page you can find the example usage for javax.mail Folder list.

Prototype

public abstract Folder[] list(String pattern) throws MessagingException;

Source Link

Document

Returns a list of Folders belonging to this Folder's namespace that match the specified pattern.

Usage

From source file:folderlist.java

public static void main(String argv[]) throws Exception {
    int optind;/*from   w w w. j a v  a2 s  .  com*/
    for (optind = 0; optind < argv.length; optind++) {
        if (argv[optind].equals("-T")) {
            protocol = argv[++optind];
        } else if (argv[optind].equals("-H")) {
            host = argv[++optind];
        } else if (argv[optind].equals("-U")) {
            user = argv[++optind];
        } else if (argv[optind].equals("-P")) {
            password = argv[++optind];
        } else if (argv[optind].equals("-L")) {
            url = argv[++optind];
        } else if (argv[optind].equals("-R")) {
            root = argv[++optind];
        } else if (argv[optind].equals("-r")) {
            recursive = true;
        } else if (argv[optind].equals("-v")) {
            verbose = true;
        } else if (argv[optind].equals("-D")) {
            debug = true;
        } else if (argv[optind].equals("--")) {
            optind++;
            break;
        } else if (argv[optind].startsWith("-")) {
            System.out.println("Usage: folderlist [-T protocol] [-H host] [-U user] [-P password] [-L url]");
            System.out.println("\t[-R root] [-r] [-v] [-D] [pattern]");
            System.exit(1);
        } else {
            break;
        }
    }
    if (optind < argv.length)
        pattern = argv[optind];

    // Get a Properties object
    Properties props = System.getProperties();

    // Get a Session object
    Session session = Session.getInstance(props, null);
    session.setDebug(debug);

    // Get a Store object
    Store store = null;
    Folder rf = null;
    if (url != null) {
        URLName urln = new URLName(url);
        store = session.getStore(urln);
        store.connect();
    } else {
        if (protocol != null)
            store = session.getStore(protocol);
        else
            store = session.getStore();

        // Connect
        if (host != null || user != null || password != null)
            store.connect(host, user, password);
        else
            store.connect();
    }

    // List namespace
    if (root != null)
        rf = store.getFolder(root);
    else
        rf = store.getDefaultFolder();

    dumpFolder(rf, false, "");
    if ((rf.getType() & Folder.HOLDS_FOLDERS) != 0) {
        Folder[] f = rf.list(pattern);
        for (int i = 0; i < f.length; i++)
            dumpFolder(f[i], recursive, "    ");
    }

    store.close();
}

From source file:com.cubusmail.mail.imap.IMAPMailbox.java

/**
 * @throws MessagingException/* ww w . java2  s .c o m*/
 */
private void loadMailFolder() throws MessagingException {

    logger.debug("loading folder tree...");
    long millis = System.currentTimeMillis();
    this.mailFolderMap.clear();
    this.mailFolderList.clear();
    Folder defaultFolder = this.store.getDefaultFolder();
    this.folderSeparator = defaultFolder.getSeparator();

    // read all folders to a map
    List<String> topFolderNames = new ArrayList<String>();
    Folder[] allFolders = defaultFolder.list("*");
    if (allFolders != null && allFolders.length > 0) {
        for (Folder folder : allFolders) {
            this.mailFolderMap.put(folder.getFullName(), createMailFolder(folder));
            if (SessionManager.get().getPreferences().getInboxFolderName().equals(folder.getFullName())) {
                topFolderNames.add(0, SessionManager.get().getPreferences().getInboxFolderName());
            } else {
                String folderName = folder.getFullName();
                if (!StringUtils.isEmpty(this.personalNameSpace)
                        && folderName.startsWith(this.personalNameSpace)) {
                    folderName = StringUtils.substringAfter(folderName, this.personalNameSpace);
                }
                if (StringUtils.countMatches(folderName, String.valueOf(getFolderSeparator())) == 0) {
                    topFolderNames.add(folder.getFullName());
                }
            }
        }
    }

    // build the tree structure
    for (String folderName : topFolderNames) {
        IMailFolder mailFolder = this.mailFolderMap.get(folderName);
        this.mailFolderList.add(mailFolder);
        if (!SessionManager.get().getPreferences().getInboxFolderName().equals(folderName)
                && mailFolder.hasChildren()) {
            mailFolder.setSubfolders(getSubfolders(mailFolder));
        }
    }

    logger.debug("...finish: " + (System.currentTimeMillis() - millis) + "ms");
}

From source file:com.cubusmail.server.mail.imap.IMAPMailbox.java

/**
 * @throws MessagingException/*w  ww . j  a  va  2 s  .com*/
 */
private void loadMailFolder() throws MessagingException {

    log.debug("loading folder tree...");
    long millis = System.currentTimeMillis();
    this.mailFolderMap.clear();
    this.mailFolderList.clear();
    Folder defaultFolder = this.store.getDefaultFolder();
    this.folderSeparator = defaultFolder.getSeparator();

    // read all folders to a map
    List<String> topFolderNames = new ArrayList<String>();
    Folder[] allFolders = defaultFolder.list("*");
    if (allFolders != null && allFolders.length > 0) {
        for (Folder folder : allFolders) {
            this.mailFolderMap.put(folder.getFullName(), createMailFolder(folder));
            if (SessionManager.get().getPreferences().getInboxFolderName().equals(folder.getFullName())) {
                topFolderNames.add(0, SessionManager.get().getPreferences().getInboxFolderName());
            } else {
                String folderName = folder.getFullName();
                if (!StringUtils.isEmpty(this.personalNameSpace)
                        && folderName.startsWith(this.personalNameSpace)) {
                    folderName = StringUtils.substringAfter(folderName, this.personalNameSpace);
                }
                if (StringUtils.countMatches(folderName, String.valueOf(getFolderSeparator())) == 0) {
                    topFolderNames.add(folder.getFullName());
                }
            }
        }
    }

    // build the tree structure
    for (String folderName : topFolderNames) {
        IMailFolder mailFolder = this.mailFolderMap.get(folderName);
        this.mailFolderList.add(mailFolder);
        if (mailFolder.hasChildren()) {
            mailFolder.setSubfolders(getSubfolders(mailFolder));
        }
    }

    log.debug("...finish: " + (System.currentTimeMillis() - millis) + "ms");
}

From source file:org.xwiki.contrib.mail.internal.AbstractMailStore.java

@Override
public ArrayList<FolderItem> getFolderTree() throws MessagingException {
    getLogger().debug("getFolderTree");

    assert (getMailSource() != null);

    ArrayList<FolderItem> folderItems = new ArrayList<FolderItem>();

    Store store = getJavamailStore();/*from  w w  w . ja  va  2  s . com*/
    store.connect();
    Folder defaultFolder = store.getDefaultFolder();
    FolderItem item = new FolderItem();
    item.setIndex(0);
    item.setLevel(0);
    item.setName(defaultFolder.getName());
    item.setFullName(defaultFolder.getFullName());
    if ((defaultFolder.getType() & javax.mail.Folder.HOLDS_MESSAGES) != 0) {
        item.setMessageCount(defaultFolder.getMessageCount());
        item.setUnreadMessageCount(defaultFolder.getUnreadMessageCount());
        item.setNewMessageCount(defaultFolder.getNewMessageCount());
    }
    Folder[] folders = defaultFolder.list("*");
    if (ArrayUtils.isEmpty(folders)) {
        folders = defaultFolder.list();
    }

    getLogger().debug("Found folders {}", ArrayUtils.toString(folders));
    int index = 1;
    int level = 1;
    // TODO not really managing folders here, just listing them
    for (Folder folder : folders) {

        item = new FolderItem();
        item.setIndex(index);
        item.setLevel(level);
        item.setName(folder.getName());
        item.setFullName(folder.getFullName());
        if ((folder.getType() & javax.mail.Folder.HOLDS_MESSAGES) != 0) {
            item.setMessageCount(folder.getMessageCount());
            item.setUnreadMessageCount(folder.getUnreadMessageCount());
            item.setNewMessageCount(folder.getNewMessageCount());
            folderItems.add(item);
        }
    }

    store.close();

    return folderItems;

}

From source file:org.xwiki.contrib.mail.internal.DefaultMailReader.java

@Override
public ArrayList<FolderItem> getFolderTree() throws MessagingException {
    assert (getMailSource() != null);
    assert (getMailSource().getHostname() != null);

    ArrayList<FolderItem> folderItems = new ArrayList<FolderItem>();
    store = null;/*w  ww  .j  a  v a 2 s  .com*/
    boolean isGmail = getMailSource().getHostname() != null
            && getMailSource().getHostname().endsWith(".gmail.com");

    logger.info("Listing folders for " + getMailSource().getHostname());

    this.session = createSession(getMailSource().getProtocol(), getMailSource().getAdditionalProperties(),
            isGmail, getMailSource().isAutoTrustSSLCertificates());

    // Get a Store object
    store = session.getStore();

    // Connect to the mail account
    store.connect(getMailSource().getHostname(), getMailSource().getPort(), getMailSource().getUsername(),
            getMailSource().getPassword());
    Folder defaultFolder = store.getDefaultFolder();
    FolderItem item = new FolderItem();
    item.setIndex(0);
    item.setLevel(0);
    item.setName(defaultFolder.getName());
    item.setFullName(defaultFolder.getFullName());
    if ((defaultFolder.getType() & javax.mail.Folder.HOLDS_MESSAGES) != 0) {
        item.setMessageCount(defaultFolder.getMessageCount());
        item.setUnreadMessageCount(defaultFolder.getUnreadMessageCount());
        item.setNewMessageCount(defaultFolder.getNewMessageCount());
    }
    Folder[] folders = defaultFolder.list("*");
    int index = 1;
    int level = 1;
    // TODO not really managing folders here, just listing them
    for (Folder folder : folders) {

        item = new FolderItem();
        item.setIndex(index);
        item.setLevel(level);
        item.setName(folder.getName());
        item.setFullName(folder.getFullName());
        if ((folder.getType() & javax.mail.Folder.HOLDS_MESSAGES) != 0) {
            item.setMessageCount(folder.getMessageCount());
            item.setUnreadMessageCount(folder.getUnreadMessageCount());
            item.setNewMessageCount(folder.getNewMessageCount());
            folderItems.add(item);
        }
    }

    store.close();

    return folderItems;

}