Example usage for javax.mail Folder getName

List of usage examples for javax.mail Folder getName

Introduction

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

Prototype

public abstract String getName();

Source Link

Document

Returns the name of this Folder.

Usage

From source file:MainClass.java

public static void main(String argv[]) {
    int start = 1;
    int end = -1;
    int optind;/*www.  j ava  2s  . c om*/

    for (optind = 0; optind < argv.length; optind++) {
        if (argv[optind].equals("-T")) { // protocol
            protocol = argv[++optind];
        } else if (argv[optind].equals("-H")) { // host
            host = argv[++optind];
        } else if (argv[optind].equals("-U")) { // user
            user = argv[++optind];
        } else if (argv[optind].equals("-P")) { // password
            password = argv[++optind];
        } else if (argv[optind].equals("-L")) {
            url = argv[++optind];
        } else if (argv[optind].equals("-s")) { // Source mbox
            src = argv[++optind];
        } else if (argv[optind].equals("-d")) { // Destination mbox
            dest = argv[++optind];
        } else if (argv[optind].equals("-x")) { // Expunge ?
            expunge = true;
        } else if (argv[optind].equals("--")) {
            optind++;
            break;
        } else if (argv[optind].startsWith("-")) {
            System.out.println("Usage: mover [-T protocol] [-H host] [-U user] [-P password] [-L url] [-v]");
            System.out.println("\t[-s source mbox] [-d destination mbox] [-x] [msgnum1] [msgnum2]");
            System.out.println("\t The -x option => EXPUNGE deleted messages");
            System.out.println("\t msgnum1 => start of message-range; msgnum2 => end of message-range");
            System.exit(1);
        } else {
            break;
        }
    }

    if (optind < argv.length)
        start = Integer.parseInt(argv[optind++]); // start msg

    if (optind < argv.length)
        end = Integer.parseInt(argv[optind++]); // end msg

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

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

        // Get a Store object
        Store store = 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();
        }

        // Open source Folder
        Folder folder = store.getFolder(src);
        if (folder == null || !folder.exists()) {
            System.out.println("Invalid folder: " + folder.getName());
            System.exit(1);
        }

        folder.open(Folder.READ_WRITE);

        int count = folder.getMessageCount();
        if (count == 0) { // No messages in the source folder
            System.out.println(folder.getName() + " is empty");
            // Close folder, store and return
            folder.close(false);
            store.close();
            return;
        }

        // Open destination folder, create if reqd
        Folder dfolder = store.getFolder(dest);
        if (!dfolder.exists())
            dfolder.create(Folder.HOLDS_MESSAGES);

        if (end == -1)
            end = count;

        // Get the message objects to copy
        Message[] msgs = folder.getMessages(start, end);
        System.out.println("Moving " + msgs.length + " messages");

        if (msgs.length != 0) {
            folder.copyMessages(msgs, dfolder);
            folder.setFlags(msgs, new Flags(Flags.Flag.DELETED), true);

            // Dump out the Flags of the moved messages, to insure that
            // all got deleted
            for (int i = 0; i < msgs.length; i++) {
                if (!msgs[i].isSet(Flags.Flag.DELETED))
                    System.out.println("Message # " + msgs[i] + " not deleted");
            }
        }

        // Close folders and store
        folder.close(expunge);
        store.close();

    } catch (MessagingException mex) {
        Exception ex = mex;
        do {
            System.out.println(ex.getMessage());
            if (ex instanceof MessagingException)
                ex = ((MessagingException) ex).getNextException();
            else
                ex = null;
        } while (ex != null);
    }
}

From source file:mover.java

public static void main(String argv[]) {
    int start = 1;
    int end = -1;
    int optind;/* w w  w  .j ava  2 s. c o m*/

    for (optind = 0; optind < argv.length; optind++) {
        if (argv[optind].equals("-T")) { // protocol
            protocol = argv[++optind];
        } else if (argv[optind].equals("-H")) { // host
            host = argv[++optind];
        } else if (argv[optind].equals("-U")) { // user
            user = argv[++optind];
        } else if (argv[optind].equals("-P")) { // password
            password = argv[++optind];
        } else if (argv[optind].equals("-L")) {
            url = argv[++optind];
        } else if (argv[optind].equals("-s")) { // Source mbox
            src = argv[++optind];
        } else if (argv[optind].equals("-d")) { // Destination mbox
            dest = argv[++optind];
        } else if (argv[optind].equals("-x")) { // Expunge ?
            expunge = true;
        } else if (argv[optind].equals("--")) {
            optind++;
            break;
        } else if (argv[optind].startsWith("-")) {
            System.out.println("Usage: mover [-T protocol] [-H host] [-U user] [-P password] [-L url] [-v]");
            System.out.println("\t[-s source mbox] [-d destination mbox] [-x] [msgnum1] [msgnum2]");
            System.out.println("\t The -x option => EXPUNGE deleted messages");
            System.out.println("\t msgnum1 => start of message-range; msgnum2 => end of message-range");
            System.exit(1);
        } else {
            break;
        }
    }

    if (optind < argv.length)
        start = Integer.parseInt(argv[optind++]); // start msg

    if (optind < argv.length)
        end = Integer.parseInt(argv[optind++]); // end msg

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

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

        // Get a Store object
        Store store = 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();
        }

        // Open source Folder
        Folder folder = store.getFolder(src);
        if (folder == null || !folder.exists()) {
            System.out.println("Invalid folder: " + src);
            System.exit(1);
        }

        folder.open(Folder.READ_WRITE);

        int count = folder.getMessageCount();
        if (count == 0) { // No messages in the source folder
            System.out.println(folder.getName() + " is empty");
            // Close folder, store and return
            folder.close(false);
            store.close();
            return;
        }

        // Open destination folder, create if reqd
        Folder dfolder = store.getFolder(dest);
        if (!dfolder.exists())
            dfolder.create(Folder.HOLDS_MESSAGES);

        if (end == -1)
            end = count;

        // Get the message objects to copy
        Message[] msgs = folder.getMessages(start, end);
        System.out.println("Moving " + msgs.length + " messages");

        if (msgs.length != 0) {
            folder.copyMessages(msgs, dfolder);
            folder.setFlags(msgs, new Flags(Flags.Flag.DELETED), true);

            // Dump out the Flags of the moved messages, to insure that
            // all got deleted
            for (int i = 0; i < msgs.length; i++) {
                if (!msgs[i].isSet(Flags.Flag.DELETED))
                    System.out.println("Message # " + msgs[i] + " not deleted");
            }
        }

        // Close folders and store
        folder.close(expunge);
        store.close();

    } catch (MessagingException mex) {
        Exception ex = mex;
        do {
            System.out.println(ex.getMessage());
            if (ex instanceof MessagingException)
                ex = ((MessagingException) ex).getNextException();
            else
                ex = null;
        } while (ex != null);
    }
}

From source file:populate.java

public static void main(String argv[]) {
    String srcURL = null;//from w  ww.j a v  a2  s  . com
    String dstURL = null;
    boolean debug = false;

    int optind;

    for (optind = 0; optind < argv.length; optind++) {
        if (argv[optind].equals("-s")) {
            srcURL = argv[++optind];
        } else if (argv[optind].equals("-d")) {
            dstURL = argv[++optind];
        } else if (argv[optind].equals("-D")) {
            debug = true;
        } else if (argv[optind].equals("-f")) {
            force = true;
        } else if (argv[optind].equals("-S")) {
            skipSpecial = true;
        } else if (argv[optind].equals("-c")) {
            clear = true;
        } else if (argv[optind].equals("-P")) {
            dontPreserveFlags = true;
        } else if (argv[optind].equals("-W")) {
            warn = true;
        } else if (argv[optind].equals("--")) {
            optind++;
            break;
        } else if (argv[optind].startsWith("-")) {
            printUsage();
            System.exit(1);
        } else {
            break;
        }
    }

    try {

        if (srcURL == null || dstURL == null) {
            printUsage();
            System.exit(1);
        }

        Session session = Session.getInstance(System.getProperties(), null);
        session.setDebug(debug);

        // Get source folder
        URLName srcURLName = new URLName(srcURL);
        Folder srcFolder;
        // Check if the source URL has a folder specified. If
        // not, we use the default folder
        if (srcURLName.getFile() == null) {
            Store s = session.getStore(srcURLName);
            s.connect();
            srcFolder = s.getDefaultFolder();
        } else {
            srcFolder = session.getFolder(new URLName(srcURL));
            if (!srcFolder.exists()) {
                System.out.println("source folder does not exist");
                srcFolder.getStore().close();
                System.exit(1);
            }
        }

        // Set up destination folder
        URLName dstURLName = new URLName(dstURL);
        Folder dstFolder;
        // Check if the destination URL has a folder specified. If
        // not, we use the source folder name
        if (dstURLName.getFile() == null) {
            Store s = session.getStore(dstURLName);
            s.connect();
            dstFolder = s.getFolder(srcFolder.getName());
        } else
            dstFolder = session.getFolder(dstURLName);

        if (clear && dstFolder.exists()) {
            if (!dstFolder.delete(true)) {
                System.out.println("couldn't delete " + dstFolder.getFullName());
                return;
            }
        }
        copy(srcFolder, dstFolder);

        // Close the respective stores.
        srcFolder.getStore().close();
        dstFolder.getStore().close();

    } catch (MessagingException mex) {
        System.out.println(mex.getMessage());
        mex.printStackTrace();
    }
}

From source file:populate.java

public static void main(String argv[]) {
    String srcURL = null;/*w w  w . j av a  2s  .  c om*/
    String dstURL = null;
    boolean debug = false;

    int optind;

    for (optind = 0; optind < argv.length; optind++) {
        if (argv[optind].equals("-s")) {
            srcURL = argv[++optind];
        } else if (argv[optind].equals("-d")) {
            dstURL = argv[++optind];
        } else if (argv[optind].equals("-D")) {
            debug = true;
        } else if (argv[optind].equals("-f")) {
            force = true;
        } else if (argv[optind].equals("-S")) {
            skipSpecial = true;
        } else if (argv[optind].equals("-c")) {
            clear = true;
        } else if (argv[optind].equals("-P")) {
            dontPreserveFlags = true;
        } else if (argv[optind].equals("--")) {
            optind++;
            break;
        } else if (argv[optind].startsWith("-")) {
            printUsage();
            System.exit(1);
        } else {
            break;
        }
    }

    try {

        if (srcURL == null || dstURL == null) {
            printUsage();
            System.exit(1);
        }

        Session session = Session.getInstance(System.getProperties(), null);
        session.setDebug(debug);

        // Get source folder
        URLName srcURLName = new URLName(srcURL);
        Folder srcFolder;
        // Check if the source URL has a folder specified. If
        // not, we use the default folder
        if (srcURLName.getFile() == null) {
            Store s = session.getStore(srcURLName);
            s.connect();
            srcFolder = s.getDefaultFolder();
        } else {
            srcFolder = session.getFolder(new URLName(srcURL));
            if (!srcFolder.exists()) {
                System.out.println("source folder does not exist");
                srcFolder.getStore().close();
                System.exit(1);
            }
        }

        // Set up destination folder
        URLName dstURLName = new URLName(dstURL);
        Folder dstFolder;
        // Check if the destination URL has a folder specified. If
        // not, we use the source folder name
        if (dstURLName.getFile() == null) {
            Store s = session.getStore(dstURLName);
            s.connect();
            dstFolder = s.getFolder(srcFolder.getName());
        } else
            dstFolder = session.getFolder(dstURLName);

        if (clear && dstFolder.exists()) {
            if (!dstFolder.delete(true)) {
                System.out.println("couldn't delete " + dstFolder.getFullName());
                return;
            }
        }
        copy(srcFolder, dstFolder);

        // Close the respective stores.
        srcFolder.getStore().close();
        dstFolder.getStore().close();

    } catch (MessagingException mex) {
        System.out.println(mex.getMessage());
        mex.printStackTrace();
    }
}

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());
        }//  w ww .  j a va 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   ww w.j  a  v  a2 s.c om
        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:com.treesbearfruit.icloudnotes.NotesSaver.java

public NotesSaver(String username, String password, String j, String wheretosave) throws Exception {

    // Get system properties
    Properties props = System.getProperties();

    Session session = Session.getDefaultInstance(props);

    // Get the store
    Store store = session.getStore("imaps");
    String noteFolderLabel = "";

    if (j.toLowerCase().equals("apple")) {
        noteFolderLabel = "Notes";
        store.connect("imap.mail.me.com", username, password); // username without @icloud.com
    } else if (j.toLowerCase().equals("google")) {
        noteFolderLabel = "Notes";
        store.connect("imap.gmail.com", username, password);
    } else {/* www.  ja v a  2 s  .  c o m*/
        throw new Exception("Notesprovider not implemented!");
    }

    String timestamp = new java.text.SimpleDateFormat("dd_MM_yyyy_hh_mm_ss").format(new Date());
    String backup_directory = wheretosave + (wheretosave.endsWith(File.separator) ? "" : File.separator)
            + noteFolderLabel + "_" + timestamp + File.separator;

    // saves main folder
    save(store, backup_directory, noteFolderLabel);

    // folder..s   
    Folder mainnotefolder = store.getFolder(noteFolderLabel);
    System.out.println("found " + mainnotefolder.list().length + " note folders");
    Folder[] f = mainnotefolder.list();
    for (Folder fd : f) {
        String backup_directory_i = backup_directory + fd.getName();
        save(store, backup_directory_i, noteFolderLabel + "/" + fd.getName());
    }

    // Close connection
    store.close();
}

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  w w  w .ja  v a  2  s .c om
    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.googlecode.gmail4j.javamail.ImapGmailClient.java

/**
 * Close any {@link Folder} that contain {@link Message} and are in open state.
 *
 * @param folder {@link Folder} to be closed
 *///from   www.  j  av a 2 s  .c om
private void closeFolder(Folder folder) {
    if (folder != null) {
        try {
            if (folder.isOpen()) {
                folder.close(true);
            } else {
                LOG.info(folder.getName() + " folder is already open.");
            }
        } catch (Exception e) {
            LOG.warn("Cannot close folder : " + folder.getName(), e);
        }
    }
}

From source file:com.alvexcore.repo.emails.impl.ExtendedEmailMessage.java

protected void fetchFolderChildren(Folder folder, NodeRef repoFolder) throws MessagingException {
    for (Folder f : folder.list()) {
        Map<QName, Serializable> props = new HashMap<QName, Serializable>();
        props.put(AlvexContentModel.PROP_EMAIL_FOLDER_FETCH_ACTIVE, true);
        props.put(ContentModel.PROP_NAME, f.getName());
        NodeRef newNode = nodeService.createNode(repoFolder, ContentModel.ASSOC_CONTAINS,
                QName.createQName(AlvexContentModel.ALVEXEM_MODEL_URI, f.getName()),
                AlvexContentModel.TYPE_EMAIL_FOLDER, props).getChildRef();
        fetchFolderChildren(f, newNode);
    }/*w  w w.java  2s.  c  om*/
}