Example usage for javax.mail Folder setSubscribed

List of usage examples for javax.mail Folder setSubscribed

Introduction

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

Prototype

public void setSubscribed(boolean subscribe) throws MessagingException 

Source Link

Document

Subscribe or unsubscribe this Folder.

Usage

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

public IMailFolder createFolder(String parentFolderId, String folderName) throws MailFolderException {

    try {//w  ww. ja va  2 s  .  c  o  m

        String newFolderName = null;
        if (!StringUtils.isEmpty(parentFolderId)) {
            newFolderName = parentFolderId + getFolderSeparator() + folderName;
        } else {
            newFolderName = folderName;
        }

        Folder newFolder = this.store.getFolder(newFolderName);
        if (!newFolder.exists()) {
            log.debug("Creating folder... " + newFolderName);
            boolean success = newFolder.create(Folder.HOLDS_MESSAGES);
            if (!success) {
                throw new MailFolderException(IErrorCodes.EXCEPTION_FOLDER_CREATE, null);
            }
            newFolder.setSubscribed(true);
        } else {
            throw new MailFolderException(IErrorCodes.EXCEPTION_FOLDER_ALREADY_EXIST, null);
        }
        loadMailFolder();

        return createMailFolder(newFolder);
    } catch (MessagingException ex) {
        throw new MailFolderException(IErrorCodes.EXCEPTION_FOLDER_CREATE, ex);
    }
}

From source file:net.wastl.webmail.server.WebMailSession.java

/**
 * Refresh Information about folders.// ww  w. j a v a  2 s. co m
 * Tries to connect folders that are not yet connected.
 *
 * @doCount display message counts for user
 */
public void refreshFolderInformation(boolean subscribed_only, boolean doCount) {
    /* Right now, doCount corresponds exactly to subscribed_only.
     * When we add a user preference setting or one-time action,
     * to present messages from all folders, we will have
     * subscribed_only false and doCount true. */

    //log.fatal("Invoking refreshFolderInformation(boolean, boolean)",
    //new Throwable("Thread Dump"));  FOR DEBUGGING
    setEnv();
    if (folders == null)
        folders = new Hashtable<String, Folder>();
    Folder rootFolder = null;
    String cur_mh_id = "";
    Enumeration mailhosts = user.mailHosts();
    int max_depth = 0;
    int folderType;

    while (mailhosts.hasMoreElements()) {
        cur_mh_id = (String) mailhosts.nextElement();

        MailHostData mhd = user.getMailHost(cur_mh_id);

        URLName url = new URLName(mhd.getHostURL());

        Element mailhost = model.createMailhost(mhd.getName(), mhd.getID(), url.toString());

        int depth = 0;

        try {
            rootFolder = getRootFolder(cur_mh_id);

            try {
                rootFolder.setSubscribed(true);
            } catch (MessagingException ex) {
                // Only IMAP supports subscription
                log.warn("Folder.setSubscribed failed.  " + "Probably a non-supporting mail service: " + ex);
            }
        } catch (MessagingException ex) {
            mailhost.setAttribute("error", ex.getMessage());
            log.warn("Failed to connect and get Root folder from (" + url + ')', ex);
            return;
        }

        try {
            depth = getFolderTree(rootFolder.getFolder("INBOX"), mailhost, subscribed_only, doCount);
            log.debug("Loaded INBOX folders below Root to a depth of " + depth);
            String extraFolderPath = ((imapBasedir == null) ? "~" : imapBasedir) + mhd.getLogin();
            //String extraFolderPath = "/home/" + mhd.getLogin();
            Folder nonInboxBase = rootFolder.getFolder(extraFolderPath);
            log.debug("Trying extra base dir " + nonInboxBase.getFullName());
            if (nonInboxBase.exists()) {
                folderType = nonInboxBase.getType();
                if ((folderType & Folder.HOLDS_MESSAGES) != 0) {
                    // Can only Subscribe to Folders which may hold Msgs.
                    nonInboxBase.setSubscribed(true);
                    if (!nonInboxBase.isSubscribed())
                        log.error("A bug in JavaMail or in the server is " + "preventing subscription to '"
                                + nonInboxBase.getFullName() + "' on '" + url
                                + "'.  Folders will not be visible.");
                }
                int extraDepth = extraDepth = getFolderTree(nonInboxBase, mailhost, subscribed_only, doCount);
                if (extraDepth > depth)
                    depth = extraDepth;
                log.debug("Loaded additional folders from below " + nonInboxBase.getFullName()
                        + " with max depth of " + extraDepth);
            }
        } catch (Exception ex) {
            if (!url.getProtocol().startsWith("pop"))
                mailhost.setAttribute("error", ex.getMessage());
            log.warn("Failed to fetch child folders from (" + url + ')', ex);
        }
        if (depth > max_depth)
            max_depth = depth;
        model.addMailhost(mailhost);
    }
    model.setStateVar("max folder depth", (1 + max_depth) + "");
}

From source file:net.wastl.webmail.server.WebMailSession.java

/**
 * Try to subscribe to a folder (i.e. unhide it)
 *///w w  w.j a  v a 2  s .  c om
public void subscribeFolder(String folderhash) {
    Folder folder = getFolder(folderhash);

    // Only IMAP supports subscription...
    try {
        folder.setSubscribed(true);
    } catch (MessagingException ex) {
        log.warn("Folder subscription not supported");
    }
}

From source file:net.wastl.webmail.server.WebMailSession.java

/**
 * Try to unsubscribe from a folder (i.e. hide it)
 *//*from ww w.  j av a 2 s . c  o m*/
public void unsubscribeFolder(String folderhash) {
    Folder folder = getFolder(folderhash);

    // Only IMAP supports subscription...
    try {
        folder.setSubscribed(false);
    } catch (MessagingException ex) {
        log.warn("Folder subscription not supported");
    }
}

From source file:net.wastl.webmail.server.WebMailSession.java

/**
 * Subscribe all folders for a Mailhost/*  ww  w .  ja va2 s  .  co 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:net.wastl.webmail.server.WebMailSession.java

/**
 * Subscribe the root folder./*from  w w  w.ja v a 2s  .  co m*/
 *
 * Preferable over setSubscribedAll
 */
public void setSubscribedDefault(String id, boolean subscribed) throws MessagingException {
    Folder folder = getRootFolder(id);
    folder.setSubscribed(subscribed);
}