Example usage for javax.mail Folder getType

List of usage examples for javax.mail Folder getType

Introduction

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

Prototype

public abstract int getType() throws MessagingException;

Source Link

Document

Returns the type of this Folder, that is, whether this folder can hold messages or subfolders or both.

Usage

From source file:folderlist.java

public static void main(String argv[]) throws Exception {
    int optind;/*  w w w .  j  a  v  a2  s .co m*/
    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:populate.java

private static void copy(Folder src, Folder dst) throws MessagingException {
    System.out.println("Populating " + dst.getFullName());

    Folder ddst = dst;//from   ww  w. j  av a2 s  . c  o m
    Folder[] srcFolders = null;
    if ((src.getType() & Folder.HOLDS_FOLDERS) != 0)
        srcFolders = src.list();
    boolean srcHasChildren = srcFolders != null && srcFolders.length > 0;

    if (!dst.exists()) {
        // Create it.
        boolean dstHoldsOnlyFolders = false;
        if (!dst.create(src.getType())) {
            // might not be able to create a folder that holds both
            if (!dst.create(srcHasChildren ? Folder.HOLDS_FOLDERS : Folder.HOLDS_MESSAGES)) {
                // might only be able to create one type (Gmail)
                if (!dst.create(Folder.HOLDS_MESSAGES)) {
                    System.out.println("couldn't create " + dst.getFullName());
                    return;
                }
            }
            dstHoldsOnlyFolders = srcHasChildren;
        }

        // Copy over any messges from src to dst
        if ((src.getType() & Folder.HOLDS_MESSAGES) != 0) {
            src.open(Folder.READ_ONLY);
            if (dstHoldsOnlyFolders) {
                if (src.getMessageCount() > 0) {
                    System.out.println("Unable to copy messages from " + src.getFullName() + " to "
                            + dst.getFullName() + " because destination holds only folders");
                }
            } else
                copyMessages(src, dst);
            src.close(false);
        }
    } else {
        System.out.println(dst.getFullName() + " already exists");
        // Copy over any messges from src to dst
        if (force && (src.getType() & Folder.HOLDS_MESSAGES) != 0) {
            src.open(Folder.READ_ONLY);
            copyMessages(src, dst);
            src.close(false);
        }
    }

    // Copy over subfolders
    if (srcHasChildren) {
        for (int i = 0; i < srcFolders.length; i++) {
            // skip special directories?
            if (skipSpecial) {
                if (srcFolders[i].getName().equals("SCCS") || srcFolders[i].getName().equals("Drafts")
                        || srcFolders[i].getName().equals("Trash")
                        || srcFolders[i].getName().equals("Shared Folders"))
                    continue;
            }
            copy(srcFolders[i], dst.getFolder(srcFolders[i].getName()));
        }
    }
}

From source file:populate.java

private static void copy(Folder src, Folder dst) throws MessagingException {
    System.out.println("Populating " + dst.getFullName());

    Folder ddst = dst;//from ww  w .j  av  a  2s .co  m
    Folder[] srcFolders = null;
    if ((src.getType() & Folder.HOLDS_FOLDERS) != 0)
        srcFolders = src.list();
    boolean srcHasChildren = srcFolders != null && srcFolders.length > 0;

    if (!dst.exists()) {
        // Create it.
        boolean dstHoldsOnlyFolders = false;
        try {
            if (!dst.create(src.getType())) {
                System.out.println("couldn't create " + dst.getFullName());
                return;
            }
        } catch (MessagingException mex) {
            // might not be able to create a folder that holds both
            if (src.getType() != (Folder.HOLDS_MESSAGES | Folder.HOLDS_FOLDERS))
                throw mex;
            if (!dst.create(srcHasChildren ? Folder.HOLDS_FOLDERS : Folder.HOLDS_MESSAGES)) {
                System.out.println("couldn't create " + dst.getFullName());
                return;
            }
            dstHoldsOnlyFolders = srcHasChildren;
        }

        // Copy over any messges from src to dst
        if ((src.getType() & Folder.HOLDS_MESSAGES) != 0) {
            src.open(Folder.READ_ONLY);
            if (dstHoldsOnlyFolders) {
                if (src.getMessageCount() > 0) {
                    System.out.println("Unable to copy messages from " + src.getFullName() + " to "
                            + dst.getFullName() + " because destination holds only folders");
                }
            } else
                copyMessages(src, dst);
            src.close(false);
        }
    } else {
        System.out.println(dst.getFullName() + " already exists");
        // Copy over any messges from src to dst
        if (force && (src.getType() & Folder.HOLDS_MESSAGES) != 0) {
            src.open(Folder.READ_ONLY);
            copyMessages(src, dst);
            src.close(false);
        }
    }

    // Copy over subfolders
    if (srcHasChildren) {
        for (int i = 0; i < srcFolders.length; i++) {
            // skip special directories?
            if (skipSpecial) {
                if (srcFolders[i].getName().equals("SCCS") || srcFolders[i].getName().equals("Drafts")
                        || srcFolders[i].getName().equals("Trash")
                        || srcFolders[i].getName().equals("Shared Folders"))
                    continue;
            }
            copy(srcFolders[i], dst.getFolder(srcFolders[i].getName()));
        }
    }
}

From source file:folderlist.java

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 (verbose) {
        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());
        }/*from w ww .ja v a  2  s.co m*/
        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 + "    ");
        }
    }
}

From source file:MainClass.java

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 (verbose) {
        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());
        }//from  w  w  w .j  a  va 2 s  .  com
        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 + "    ");
        }
    }
}

From source file:SimpleClient.java

public void valueChanged(TreeSelectionEvent e) {
    TreePath path = e.getNewLeadSelectionPath();
    if (path != null) {
        Object o = path.getLastPathComponent();
        if (o instanceof FolderTreeNode) {
            FolderTreeNode node = (FolderTreeNode) o;
            Folder folder = node.getFolder();

            try {
                if ((folder.getType() & Folder.HOLDS_MESSAGES) != 0) {
                    SimpleClient.fv.setFolder(folder);
                }//ww w.ja va 2 s . co m
            } catch (MessagingException me) {
            }
        }
    }
}

From source file:org.jasig.portlet.emailpreview.controller.AjaxUpdateInboxFolderController.java

@ResourceMapping("inboxFolder")
public ModelAndView inboxFolder(ResourceRequest req, ResourceResponse res) throws MessagingException {
    IEmailAccountService accountDao = serviceBroker.getEmailAccountService(req);

    PortletPreferences prefs = req.getPreferences();
    String selectedFolder = prefs.getValue(EmailAccountSummaryController.INBOX_NAME_PREFERENCE,
            INBOX_DEFAULT_VALUE_PREF);/*  w w w.  j a  va  2  s  .  c  o  m*/
    String authenticationServiceKey = prefs.getValue(AUTH_SERVICE_PREFERENCE,
            AUTH_SERVICE_DEFAULT_VALUE_PREFERENCE);

    Map<String, String> jsonData = new Hashtable<String, String>();

    if (AUTH_SERVICE_TEST_PREFERENCE.equals(authenticationServiceKey)) {
        jsonData.put("INBOX", "INBOX");
        //For demo : must have the same name that the json files in src/main/resources
        jsonData.put("demoTest", "demoTest");
        jsonData.put("Important", "Important");
        for (Map.Entry<String, String> entry : jsonData.entrySet()) {
            if (entry.getValue().equals(selectedFolder)) {
                jsonData.put(SELECTED_OPTION, entry.getValue());
                break;
            }
        }
    } else {
        for (Folder folderName : accountDao.getAllUserInboxFolders(req)) {
            if ((folderName.getType() & javax.mail.Folder.HOLDS_MESSAGES) != 0) {
                String unreadMsgCount = "";
                if (folderName.getUnreadMessageCount() != 0
                        && (!selectedFolder.equals(folderName.toString()))) {
                    unreadMsgCount = " (".concat(String.valueOf(folderName.getUnreadMessageCount())).concat(")")
                            .concat(UNREAD_MSG_FLAG);
                }
                jsonData.put(folderName.toString().concat(unreadMsgCount), folderName.toString());
                if (selectedFolder.equals(folderName.toString())) {
                    jsonData.put(SELECTED_OPTION, folderName.toString());
                }
            }
        }
    }
    return new ModelAndView("json", jsonData);
}

From source file:com.jaeksoft.searchlib.crawler.mailbox.crawler.MailboxAbstractCrawler.java

protected void readFolder(Folder folder) throws MessagingException, IOException, SearchLibException {
    if (folder == null)
        return;//from   w  w  w. java  2s .  c om
    if ((folder.getType() & Folder.HOLDS_MESSAGES) != 0)
        readMessagesFolder(folder);
    if ((folder.getType() & Folder.HOLDS_FOLDERS) != 0)
        readHoldsFolder(folder);
}

From source file:com.jaeksoft.searchlib.crawler.mailbox.crawler.MailboxAbstractCrawler.java

protected void checkFolder(Folder folder, PrintWriter pw)
        throws MessagingException, IOException, SearchLibException {
    if (folder == null)
        return;/*from  ww  w  .j a v a2  s .  co  m*/
    if ((folder.getType() & Folder.HOLDS_MESSAGES) != 0) {
        folder.open(Folder.READ_ONLY);
        try {
            pw.print("Folder ");
            pw.print(folder.getName());
            pw.print(": ");
            pw.print(folder.getMessageCount());
            pw.println(" msgs(s).");
        } finally {
            folder.close(false);
        }
    }
    if ((folder.getType() & Folder.HOLDS_FOLDERS) != 0) {
        Folder[] folders = folder.list();
        if (folders != null)
            for (Folder f : folders)
                checkFolder(f, pw);
    }
}

From source file:com.naryx.tagfusion.cfm.mail.cfMailMessageData.java

public void getMessage(cfImapConnection imapConnection, String rootFolder, long messageID, String _attachURI,
        String _attachDIR) throws cfmRunTimeException {
    try {//from w ww.j  ava 2 s  .  c  om
        Folder folderToList;

        if (rootFolder == null || rootFolder.length() == 0)
            folderToList = imapConnection.mailStore.getDefaultFolder();
        else
            folderToList = imapConnection.mailStore.getFolder(rootFolder);

        if ((folderToList.getType() & Folder.HOLDS_MESSAGES) != 0) {

            if (!folderToList.isOpen())
                folderToList.open(Folder.READ_ONLY);

            boolean bResult = false;
            if (folderToList instanceof UIDFolder)
                bResult = extractMessage(((UIDFolder) folderToList).getMessageByUID(messageID), messageID,
                        _attachURI, _attachDIR);
            else
                bResult = extractMessage(folderToList.getMessage((int) messageID), messageID, _attachURI,
                        _attachDIR);

            if (!bResult)
                imapConnection.setStatus(false, "Message does not exist");
            else
                imapConnection.setStatus(true, "");

            folderToList.close(false);
        }

    } catch (Exception E) {
        imapConnection.setStatus(false, E.getMessage());
    }
}