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:be.ibridge.kettle.job.entry.getpop.JobEntryGetPOP.java

public Result execute(Result prev_result, int nr, Repository rep, Job parentJob) {
    LogWriter log = LogWriter.getInstance();
    Result result = new Result(nr);
    result.setResult(false);/*  w ww  . j  a  v  a2 s .c  o  m*/
    result.setNrErrors(1);

    FileObject fileObject = null;

    //Get system properties

    //Properties prop = System.getProperties();
    Properties prop = new Properties();

    //Create session object
    //Session sess = Session.getDefaultInstance(prop,null);
    Session sess = Session.getInstance(prop, null);
    sess.setDebug(true);

    try {

        int nbrmailtoretrieve = Const.toInt(firstmails, 0);
        fileObject = KettleVFS.getFileObject(getRealOutputDirectory());

        // Check if output folder exists
        if (!fileObject.exists()) {
            log.logError(toString(),
                    Messages.getString("JobGetMailsFromPOP.FolderNotExists1.Label") + getRealOutputDirectory()
                            + Messages.getString("JobGetMailsFromPOP.FolderNotExists2.Label"));
        } else {

            String host = getRealServername();
            String user = getRealUsername();
            String pwd = getRealPassword();

            Store st = null;

            if (!getUseSSL()) {

                //Create POP3 object               
                st = sess.getStore("pop3");

                // Try to connect to the server
                st.connect(host, user, pwd);
            } else {
                // Ssupports POP3 connection with SSL, the connection is established via SSL.

                String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

                //Properties pop3Props = new Properties();

                prop.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
                prop.setProperty("mail.pop3.socketFactory.fallback", "false");
                prop.setProperty("mail.pop3.port", getRealSSLPort());
                prop.setProperty("mail.pop3.socketFactory.port", getRealSSLPort());

                URLName url = new URLName("pop3", host, Const.toInt(getRealSSLPort(), 995), "", user, pwd);

                st = new POP3SSLStore(sess, url);

                st.connect();

            }

            log.logDetailed(toString(), Messages.getString("JobGetMailsFromPOP.LoggedWithUser.Label") + user);

            //Open default folder   INBOX 
            Folder f = st.getFolder("INBOX");
            f.open(Folder.READ_ONLY);

            if (f == null) {
                log.logError(toString(), Messages.getString("JobGetMailsFromPOP.InvalidFolder.Label"));

            } else {

                log.logDetailed(toString(),
                        Messages.getString("JobGetMailsFromPOP.TotalMessagesFolder1.Label") + f.getName()
                                + Messages.getString("JobGetMailsFromPOP.TotalMessagesFolder2.Label")
                                + f.getMessageCount());
                log.logDetailed(toString(),
                        Messages.getString("JobGetMailsFromPOP.TotalNewMessagesFolder1.Label") + f.getName()
                                + Messages.getString("JobGetMailsFromPOP.TotalNewMessagesFolder2.Label")
                                + f.getNewMessageCount());

                // Get emails 
                Message msg_list[] = getPOPMessages(f, retrievemails);

                if (msg_list.length > 0) {
                    List current_file_POP = new ArrayList();
                    List current_filepath_POP = new ArrayList();
                    int nb_email_POP = 1;
                    DateFormat dateFormat = new SimpleDateFormat("hhmmss_mmddyyyy");

                    String startpattern = "name";
                    if (!Const.isEmpty(getRealFilenamePattern())) {
                        startpattern = getRealFilenamePattern();
                    }

                    for (int i = 0; i < msg_list.length; i++)

                    {

                        /*if(msg[i].isMimeType("text/plain"))
                         {
                         log.logDetailed(toString(), "Expediteur: "+msg[i].getFrom()[0]);
                         log.logDetailed(toString(), "Sujet: "+msg[i].getSubject());
                         log.logDetailed(toString(), "Texte: "+(String)msg[i].getContent());
                                
                         }*/

                        if ((nb_email_POP <= nbrmailtoretrieve && retrievemails == 2) || (retrievemails != 2)) {

                            Message msg_POP = msg_list[i];
                            log.logDetailed(toString(), Messages.getString("JobGetMailsFromPOP.EmailFrom.Label")
                                    + msg_list[i].getFrom()[0]);
                            log.logDetailed(toString(),
                                    Messages.getString("JobGetMailsFromPOP.EmailSubject.Label")
                                            + msg_list[i].getSubject());

                            String localfilename_message = startpattern + "_" + dateFormat.format(new Date())
                                    + "_" + (i + 1) + ".mail";

                            log.logDetailed(toString(),
                                    Messages.getString("JobGetMailsFromPOP.LocalFilename1.Label")
                                            + localfilename_message
                                            + Messages.getString("JobGetMailsFromPOP.LocalFilename2.Label"));

                            File filename_message = new File(getRealOutputDirectory(), localfilename_message);
                            OutputStream os_filename = new FileOutputStream(filename_message);
                            Enumeration enums_POP = msg_POP.getAllHeaders();
                            while (enums_POP.hasMoreElements())

                            {
                                Header header_POP = (Header) enums_POP.nextElement();
                                os_filename.write(new StringBuffer(header_POP.getName()).append(": ")
                                        .append(header_POP.getValue()).append("\r\n").toString().getBytes());
                            }
                            os_filename.write("\r\n".getBytes());
                            InputStream in_POP = msg_POP.getInputStream();
                            byte[] buffer_POP = new byte[1024];
                            int length_POP = 0;
                            while ((length_POP = in_POP.read(buffer_POP, 0, 1024)) != -1) {
                                os_filename.write(buffer_POP, 0, length_POP);
                            }
                            os_filename.close();
                            nb_email_POP++;
                            current_file_POP.add(filename_message);
                            current_filepath_POP.add(filename_message.getPath());

                            if (delete) {
                                log.logDetailed(toString(),
                                        Messages.getString("JobGetMailsFromPOP.DeleteEmail.Label"));
                                msg_POP.setFlag(javax.mail.Flags.Flag.DELETED, true);
                            }
                        }

                    }
                }
                // Close and exit 
                if (f != null)
                    f.close(false);
                if (st != null)
                    st.close();

                f = null;
                st = null;
                sess = null;

                result.setNrErrors(0);
                result.setResult(true);

            }
        }

    }

    catch (NoSuchProviderException e) {
        log.logError(toString(), "provider error: " + e.getMessage());
    } catch (MessagingException e) {
        log.logError(toString(), "Message error: " + e.getMessage());
    }

    catch (Exception e) {
        log.logError(toString(), "Inexpected error: " + e.getMessage());
    }

    finally {
        if (fileObject != null) {
            try {
                fileObject.close();
            } catch (IOException ex) {
            }
            ;
        }
        sess = null;

    }

    return result;
}

From source file:com.sonicle.webtop.mail.MailAccount.java

public FolderCache moveFolder(String source, String dest) throws MessagingException {
    Folder oldfolder = getFolder(source);
    String oldname = oldfolder.getName();
    //System.out.println("moveFolder "+source+" -> "+dest);
    Folder newfolder;/*from www.  j a va  2 s .c om*/
    if (dest != null && dest.trim().length() > 0) {
        String newname = dest + folderSeparator + oldname;
        newfolder = getFolder(newname);
    } else {
        if (hasDifferentDefaultFolder) {
            String prefix = getDefaultFolder().getFullName();
            String newname = oldname;
            if (prefix != null)
                newname = prefix + folderSeparator + newname;
            newfolder = getFolder(newname);
        } else {
            String newname = oldname;
            if (folderPrefix != null) {
                newname = folderPrefix + newname;
            }
            newfolder = getFolder(newname);
        }
    }
    FolderCache fcsrc = getFolderCache(source);
    if (fcsrc != null) {
        destroyFolderCache(fcsrc);
    }
    boolean done = oldfolder.renameTo(newfolder);
    if (done) {
        if (dest != null) {
            FolderCache tfc = getFolderCache(newfolder.getParent().getFullName());
            return addFoldersCache(tfc, newfolder);
        } else {
            return addFoldersCache(fcRoot, newfolder);
        }
    }
    return null;
}

From source file:com.sonicle.webtop.mail.MailAccount.java

public void loadFoldersCache(final Object lock, boolean waitLoad) throws MessagingException {
    Folder froot = getDefaultFolder();/*from   ww  w.  ja  v a2s  .  c  o m*/
    fcRoot = createFolderCache(froot);
    fcRoot.setIsRoot(true);
    Folder children[] = fcRoot.getFolder().list();
    final ArrayList<FolderCache> rootParents = new ArrayList<FolderCache>();
    for (Folder child : children) {
        if (ms.isFolderHidden(this, child.getFullName()))
            continue;
        if (!fcRoot.hasChild(child.getName())) {
            FolderCache fcc = addSingleFoldersCache(fcRoot, child);
            if (!fcc.isStartupLeaf())
                rootParents.add(fcc);
        }
    }

    if (hasDifferentDefaultFolder) {
        //check for other shared folders to be added
        Folder rfolders[] = store.getDefaultFolder().list();
        for (int i = 0; i < sharedPrefixes.length; ++i) {
            for (int j = 0; j < rfolders.length; ++j) {
                if (rfolders[j].getFullName().equals(sharedPrefixes[i])) {
                    FolderCache fcc = addSingleFoldersCache(fcRoot, rfolders[j]);
                    rootParents.add(fcc);
                }
            }
        }
    }

    Thread engine = new Thread(new Runnable() {
        public void run() {
            synchronized (lock) {
                try {
                    for (FolderCache fc : rootParents) {
                        _loadFoldersCache(fc);
                    }
                } catch (MessagingException exc) {
                    Service.logger.error("Exception", exc);
                }
            }
        }
    });
    engine.start();
    try {
        if (waitLoad)
            engine.join();
    } catch (InterruptedException exc) {
        Service.logger.error("Error waiting folder cache load", exc);
    }
}

From source file:com.liferay.mail.imap.IMAPAccessor.java

public String[] addFolder(String displayName) throws MailException {
    try {//from w  w w .  j a v  a2s  .  c  o m
        String fullName = displayName;

        if (Validator.isNotNull(_account.getFolderPrefix())) {
            Store store = _imapConnection.getStore(true);

            Folder jxFolder = store.getDefaultFolder();

            fullName = _account.getFolderPrefix() + jxFolder.getSeparator() + displayName;
        }

        Folder jxFolder = getFolder(fullName);

        if (jxFolder.exists()) {
            throw new MailException(MailException.FOLDER_ALREADY_EXISTS);
        } else {
            if (jxFolder.create(Folder.HOLDS_MESSAGES)) {
                return new String[] { jxFolder.getFullName(), jxFolder.getName() };
            }

            throw new MailException(MailException.FOLDER_CREATE_FAILED);
        }
    } catch (MessagingException me) {
        throw new MailException(me);
    }
}

From source file:com.liferay.mail.imap.IMAPAccessor.java

public String[] renameFolder(long folderId, String newDisplayName) throws PortalException {

    try {/*from  ww w  . jav  a2s .  com*/
        Folder jxFolder = getFolder(folderId);

        String newFullName = StringPool.BLANK;

        if (Validator.isNotNull(_account.getFolderPrefix())) {
            Store store = _imapConnection.getStore(true);

            Folder defaultJxFolder = store.getDefaultFolder();

            newFullName = _account.getFolderPrefix() + defaultJxFolder.getSeparator() + newDisplayName;
        }

        if (!jxFolder.exists()) {
            throw new MailException(MailException.FOLDER_DOES_NOT_EXIST);
        }

        if (jxFolder.renameTo(getFolder(newFullName))) {
            return new String[] { jxFolder.getFullName(), jxFolder.getName() };
        } else {
            throw new MailException(MailException.FOLDER_RENAME_FAILED);
        }
    } catch (MessagingException me) {
        throw new MailException(me);
    }
}

From source file:edu.stanford.muse.email.EmailFetcherStats.java

/**
 * fetch given message idx's in given folder -- @performance critical
 *
 * @param offset - the original offset of the first message in the messages array, important to initialize
 *               for proper assignment of unique id or doc Id
 *///from  ww w  .j  a v  a2 s  .  com
//private void fetchUncachedMessages(String sanitizedFName, Folder folder, DocCache cache, List<Integer> msgIdxs) throws MessagingException, FileNotFoundException, IOException, GeneralSecurityException {
private void fetchAndIndexMessages(Folder folder, Message[] messages, int offset, int totalMessages)
        throws MessagingException, IOException, GeneralSecurityException {
    //mark the processing of new batch
    if (offset == 0)
        fetchStartTime = System.currentTimeMillis();

    currentStatus = JSONUtils.getStatusJSON(
            (emailStore instanceof MboxEmailStore) ? "Parsing " + folder.getName() + " (can take a while)..."
                    : "Reading " + folder.getName() + "...");

    // bulk fetch of all message headers
    int n = messages.length;

    // eliminate any messages the archive already has
    messages = removeMessagesAlreadyInArchive(archive, messages);

    log.info(n - messages.length + " message(s) already in the archive");

    ArrayList<EmailDocument> emails = new ArrayList<EmailDocument>();

    // for performance, we need to do bulk prefetches, instead of fetching 1 message at a time
    // prefetchedMessages will be a temp cache of prefetched messages
    int first_i_prefetched = -1, last_i_prefetched = -1;
    List<?> prefetchedMessages = null; // the type of this can be either list<string> if text only, otherwise list<mimemmessage>

    long highestUID = archive.getLastUIDForFolder(fetchedFolderInfo.accountKey, fetchedFolderInfo.longName);
    long lastAssignedUID = highestUID;
    boolean bodyTextOnly = !fetchConfig.downloadAttachments;
    try {
        archive.openForWrite();
        for (int i = 0; i < messages.length; i++) {
            // critical step: (thanks, yourkit!)
            // null out the ref to the previous message, otherwise it stays in memory, and the heap effectively needs to be as big as the size of all messages
            if (i > 0)
                messages[i - 1] = null;

            if (isCancelled)
                break;

            Message m = messages[i];
            MimeMessage mm = (MimeMessage) m;

            if (i >= last_i_prefetched) {
                // critical perf. step: do a bulk imap prefetch
                // the prefetch will fetch as many messages as possible up to a max buffer size, and return the messages prefetched
                // last_i_prefetched tracks what is the last index into idxs that we have prefetched.
                // when we run out of prefetched messages, we do another bulk prefetch

                prefetchedMessages = do_imap_prefetch(messages, i, folder, bodyTextOnly);
                if (prefetchedMessages != null) {
                    first_i_prefetched = i;
                    last_i_prefetched = i + prefetchedMessages.size();
                }
            }

            int pctDone = ((i + offset) * 100) / totalMessages;
            long elapsedMillis = System.currentTimeMillis() - fetchStartTime;
            long unprocessedSecs = Util.getUnprocessedMessage(i + offset, totalMessages, elapsedMillis);
            int N_TEASERS = 50; // 50 ok here, because it takes a long time to fetch and process messages, so teaser computation is relatively not expensive
            int nTriesForThisMessage = 0;
            currentStatus = getStatusJSONWithTeasers(
                    "Reading " + Util.commatize(totalMessages) + " messages from " + folder.getName() + "...",
                    pctDone, elapsedMillis / 1000, unprocessedSecs, emails, N_TEASERS);

            int messageNum = mm.getMessageNumber();

            try {
                long unique_id;

                // if we have uid, that's even better
                // don't use uid's for mbox, it has a bug and always gives -1
                // see http://james.apache.org/server/rfclist/imap4/rfc2060.txt for uid spec
                if (folder instanceof UIDFolder && !(emailStore instanceof MboxEmailStore)) {
                    long uid = ((UIDFolder) folder).getUID(m);
                    unique_id = uid;
                } else
                    unique_id = lastAssignedUID + 1 + i + offset; // +1 since i starts from 0 (but lastAssignedUID can be -1 -- is that safe? -sgh)

                if (unique_id > highestUID)
                    highestUID = unique_id;

                String unique_id_as_string = Long.toString(unique_id);

                // well, we already converted to emaildoc above during removeMessagesAlreadyInArchive
                // not a serious perf. concern now, but revisit if needed
                EmailDocument ed = convertToEmailDocument(mm, unique_id_as_string); // this messageNum is mostly for debugging, it should not be used for equals etc.
                // need to check this again, because there might be duplicates such within the set we are currently processing.
                if (archive.containsDoc(ed)) {
                    stats.nMessagesAlreadyPresent++;
                    dataErrors.add("Duplicate message: " + ed); // note: report.jsp depends on this specific string
                    continue;
                }

                MimeMessage originalMessage = mm; // this is the mm that has all the headers etc.
                List<Blob> attachmentsList = new ArrayList<Blob>();

                // if we already have it prefetched, use the prefetched version
                List<String> contents = null;

                if (first_i_prefetched >= 0 && prefetchedMessages != null) {
                    if (!fetchConfig.downloadAttachments) {
                        // text only means the prefetchedMessages are stored directly as a list of strings
                        String content = (String) prefetchedMessages.get(i - first_i_prefetched); // note: this_mm only has the prefetched content, but not the headers
                        contents = new ArrayList<String>();

                        try {
                            // a special for yahoo which routinely uses quoted-printable. content looks like  =0A0D.... = etc.
                            if (mm.isMimeType("multipart/alternative")) {
                                Multipart mm_mp = (Multipart) mm.getContent();
                                Part p0 = mm_mp.getBodyPart(0);
                                if (p0 instanceof com.sun.mail.imap.IMAPBodyPart) {
                                    String encoding = ((com.sun.mail.imap.IMAPBodyPart) p0).getEncoding();
                                    if ("quoted-printable".equals(encoding)) {
                                        content = new String(
                                                Util.getBytesFromStream(javax.mail.internet.MimeUtility.decode(
                                                        new java.io.ByteArrayInputStream(content.getBytes()),
                                                        "quoted-printable")));
                                    }
                                }
                            }
                        } catch (Exception e) {
                            Util.print_exception("Error trying to parse encoding of multipart", e, log);
                        }

                        contents.add(content);
                    } else {
                        // subtle issue here: the contentType of the prefetchedMessage needs to be be set to the original_mm's content-type.
                        // this was found for cases where the original message is multipart-alternative with a text and html part.
                        // if we don't set prefetchedMessage's content type, it gets a mime type of text/plain and a body = the entire multipart including both parts.
                        // found on sgh's sent mail w/subject: "text to add in help" from  Fri, 7 Jun 2013
                        MimeMessage prefetchedMessage = (MimeMessage) prefetchedMessages
                                .get(i - first_i_prefetched);
                        String contentTypeHeaders[] = originalMessage.getHeader("Content-Type");
                        String contentTypeHeader = null;
                        if (contentTypeHeaders != null && contentTypeHeaders.length == 1)
                            contentTypeHeader = contentTypeHeaders[0];

                        if (!Util.nullOrEmpty(contentTypeHeader)) // we do care about body structure, hang on to it
                            prefetchedMessage.setHeader("Content-Type", contentTypeHeader);
                        mm = prefetchedMessage;
                    }
                    prefetchedMessages.set(i - first_i_prefetched, null); // null out to save memory
                }

                if (contents == null)
                    contents = processMessagePart(messageNum, originalMessage, mm, attachmentsList);

                // if mm is not prefetched, it is the same as original_mm
                // will also work, but will be slow as javamail accesses and fetches each mm separately, instead of using the bulk prefetched version
                // even when prefetched, the processMessagePart is somewhat expensive because the attachments have to be extracted etc.

                // we could overlap processMessagePart with do_imap_prefetch by prefetching in a separate thread, since prefetch is network limited.
                // but profiling shows processMessagePart takes only 1/4th the time of do_imap_prefetch so overlapping would be a relatively small gain.
                // not worth the effort right now.
                ed.attachments = attachmentsList;
                if (fetchConfig.downloadAttachments)
                    ed.attachmentsYetToBeDownloaded = false; // we've already downloaded our attachments

                // concat all the contents parts
                StringBuilder sb = new StringBuilder();
                for (String s : contents) {
                    sb.append(s);
                    sb.append("\n");
                }

                String contentStr = sb.toString();
                if (!messageLooksOk(contentStr)) {
                    dataErrors.add("Skipping message as it seems to have very long words: " + ed);
                    continue;
                }

                if (contentStr.length() > Config.MAX_TEXT_SIZE_TO_ANNOTATE) {
                    dataErrors.add("Skipping message as it seems to be very long: " + contentStr.length()
                            + " chars, while the max size message that will be annotated for display is "
                            + Config.MAX_TEXT_SIZE_TO_ANNOTATE + " chars. Message = " + ed);
                    // but we continue, don't skip the message entirely. See issue #111
                }

                contentStr = IndexUtils.normalizeNewlines(contentStr); // just get rid of \r's

                archive.addDoc(ed, contentStr);

                List<LinkInfo> linkList = new ArrayList<LinkInfo>();
                // linkList might be used only for slant
                IndexUtils.populateDocLinks(ed, contentStr, linkList, true);
                ed.links = linkList;
                stats.nMessagesAdded++;
            } catch (Exception ex) {
                // sometimes we get unexpected folder closed, so try again
                boolean retry = false;
                if (ex instanceof javax.mail.FolderClosedException) {
                    log.warn("Oops, thread " + threadID + " got the folder closed in its face! "
                            + ex.getMessage());

                    // sometimes we get this exception about folder closed
                    // retry up to 3 times, then give up
                    if (nTriesForThisMessage < 3) {
                        retry = true;
                        log.info("Re-opening email store; attempt #" + (nTriesForThisMessage + 1)
                                + " for message " + i);
                        nTriesForThisMessage++;
                        messages = openFolderAndGetMessages();
                        fetchHeaders(messages);
                        --i; // adjust the message index n try again
                    }
                }

                if (!retry) {
                    // we sometimes see UnsupportedEncodingException with x-utf8utf8 mime type and ParseException
                    // nothing much can be done, just create a dummy doc and add it to the cache
                    nErrors++;
                    stats.nErrors++;
                    EmailDocument ed = new EmailDocument(Integer.toString(messageNum));
                    log.warn("Exception reading message from " + folder_name() + " Message #" + messageNum + " "
                            + ex.getMessage() + "\n" + Util.stackTrace(ex));

                    ed.setErrorString(Util.stackTrace(ex));
                }
            }
        }
    } catch (Throwable t) {
        Util.print_exception(t, log);
    } finally {
        //            if (cancelled && false) // TODO: disable for now as currently only indexes are rolled back and allDocs/blobs are not rolled back in sync yet
        //               archive.rollbackIndexWrites();
        //            else
        currentStatus = JSONUtils.getStatusJSON("Saving archive...");
        archive.close();
    }

    fetchedFolderInfo.lastSeenUID = highestUID;
    log.info("at end of fetch, folder info is " + fetchedFolderInfo);

    log.info("emailfetcher thread completed, archive has " + archive.getAllDocs().size() + " docs");
}

From source file:com.funambol.email.items.manager.ImapEntityManager.java

/**
 * updates a folder defined by folder id and parent id
 *
 * @param parentId  folder id String/*from   w  w w .j  a  v a  2s.  com*/
 * @param folderId  mail id in folder String
 * @param folderNew Folder
 * @return Folder
 * @throws EntityException
 */
public com.funambol.email.pdi.folder.Folder updateFolder(String parentId, String folderId,
        com.funambol.email.pdi.folder.Folder folderNew, Map serverItems) throws EntityException {

    com.funambol.email.pdi.folder.Folder folderOut = null;

    if (log.isTraceEnabled()) {
        log.trace("updateFolder start for FID: " + folderId);
    }

    String name = folderNew.getName().getPropertyValueAsString();
    String created = folderNew.getCreated().getPropertyValueAsString();
    String modified = folderNew.getModified().getPropertyValueAsString();
    String accessed = folderNew.getAccessed().getPropertyValueAsString();
    String attributes = null;

    ItemFolder ifout = null;

    if (folderId.equals(Def.FOLDER_INBOX_ID)) {
        ifout = new ItemFolder(Def.FOLDER_INBOX_GUID, name, Def.FOLDER_ROOT_ID, created, accessed, modified,
                Def.FOLDER_INBOX_ROLE, attributes);
        // set the values for the post update operation
        SyncItemInfo sii = createInfo(Def.FOLDER_INBOX_GUID);
        // update email to the servetItems list
        this.ied.updateItemInServerItems(sii, Def.FOLDER_INBOX_GUID, serverItems);
    } else if (folderId.equals(Def.FOLDER_OUTBOX_ID)) {
        ifout = new ItemFolder(Def.FOLDER_OUTBOX_GUID, name, Def.FOLDER_ROOT_ID, created, accessed, modified,
                Def.FOLDER_OUTBOX_ROLE, attributes);
        // set the values for the post update operation
        SyncItemInfo sii = createInfo(Def.FOLDER_OUTBOX_ID);
        // update email to the servetItems list
        this.ied.updateItemInServerItems(sii, Def.FOLDER_OUTBOX_ID, serverItems);
    } else if (folderId.equals(Def.FOLDER_SENT_ID)) {
        ifout = new ItemFolder(Def.FOLDER_SENT_GUID, name, Def.FOLDER_ROOT_ID, created, accessed, modified,
                Def.FOLDER_SENT_ROLE, attributes);
        // set the values for the post update operation
        SyncItemInfo sii = createInfo(Def.FOLDER_SENT_ID);
        // update email to the servetItems list
        this.ied.updateItemInServerItems(sii, Def.FOLDER_SENT_ID, serverItems);
    } else if (folderId.equals(Def.FOLDER_DRAFTS_ID)) {
        ifout = new ItemFolder(Def.FOLDER_DRAFTS_GUID, name, Def.FOLDER_ROOT_ID, created, accessed, modified,
                Def.FOLDER_DRAFTS_ROLE, attributes);
        // set the values for the post update operation
        SyncItemInfo sii = createInfo(Def.FOLDER_DRAFTS_ID);
        // update email to the servetItems list
        this.ied.updateItemInServerItems(sii, Def.FOLDER_DRAFTS_ID, serverItems);
    } else if (folderId.equals(Def.FOLDER_TRASH_ID)) {
        ifout = new ItemFolder(Def.FOLDER_TRASH_GUID, name, Def.FOLDER_ROOT_ID, created, accessed, modified,
                Def.FOLDER_TRASH_ROLE, attributes);
        // set the values for the post update operation
        SyncItemInfo sii = createInfo(Def.FOLDER_TRASH_GUID);
        // update email to the servetItems list
        this.ied.updateItemInServerItems(sii, Def.FOLDER_TRASH_GUID, serverItems);

    }

    // convert Folder in foundation Folder
    try {
        folderOut = createFoundationFolder(ifout);
    } catch (Exception e) {
        throw new EntityException(e);
    }

    if (log.isTraceEnabled()) {
        log.trace("updateFolder end");
    }

    return folderOut;

}

From source file:com.midori.confluence.plugin.mail2news.Mail2NewsJob.java

/**
 * The main method of this job. Called by confluence every time the mail2news trigger
 * fires./*from   w w  w .  j a v  a 2  s  .c o  m*/
 *
 * @see com.atlassian.quartz.jobs.AbstractJob#doExecute(org.quartz.JobExecutionContext)
 */
public void doExecute(JobExecutionContext arg0) throws JobExecutionException {

    /* The mailstore object used to connect to the server */
    Store store = null;

    try {

        this.log.info("Executing mail2news plugin.");

        /* check if we have all necessary components */
        if (pageManager == null) {
            throw new Exception("Null PageManager instance.");
        }
        if (spaceManager == null) {
            throw new Exception("Null SpaceManager instance.");
        }
        if (configurationManager == null) {
            throw new Exception("Null ConfigurationManager instance.");
        }

        /* get the mail configuration from the manager */
        MailConfiguration config = configurationManager.getMailConfiguration();
        if (config == null) {
            throw new Exception("Null MailConfiguration instance.");
        }

        /* create the properties for the session */
        Properties prop = new Properties();

        /* get the protocol to use */
        if (config.getProtocol() == null) {
            throw new Exception("Cannot get protocol.");
        }
        String protocol = config.getProtocol().toLowerCase().concat(config.getSecure() ? "s" : "");
        /* assemble the property prefix for this protocol */
        String propertyPrefix = "mail.";
        propertyPrefix = propertyPrefix.concat(protocol).concat(".");

        /* get the server port from the configuration and add it to the properties,
         * but only if it is actually set. If port = 0 this means we use the standard
         * port for the chosen protocol */
        int port = config.getPort();
        if (port != 0) {
            prop.setProperty(propertyPrefix.concat("port"), "" + port);
        }

        /* set connection timeout (10 seconds) */
        prop.setProperty(propertyPrefix.concat("connectiontimeout"), "10000");

        /* get the session for connecting to the mail server */
        Session session = Session.getInstance(prop, null);

        /* get the mail store, using the desired protocol */
        if (config.getSecure()) {
            store = session.getStore(protocol);
        } else {
            store = session.getStore(protocol);
        }

        /* get the host and credentials for the mail server from the configuration */
        String host = config.getServer();
        String username = config.getUsername();
        String password = config.getPassword();

        /* sanity check */
        if (host == null || username == null || password == null) {
            throw new Exception("Incomplete mail configuration settings (at least one setting is null).");
        }

        /* connect to the mailstore */
        try {
            store.connect(host, username, password);
        } catch (AuthenticationFailedException afe) {
            throw new Exception("Authentication for mail store failed: " + afe.getMessage(), afe);
        } catch (MessagingException me) {
            throw new Exception("Connecting to mail store failed: " + me.getMessage(), me);
        } catch (IllegalStateException ise) {
            throw new Exception("Connecting to mail store failed, already connected: " + ise.getMessage(), ise);
        } catch (Exception e) {
            throw new Exception("Connecting to mail store failed, general exception: " + e.getMessage(), e);
        }

        /***
         * Open the INBOX
         ***/

        /* get the INBOX folder */
        Folder folderInbox = store.getFolder("INBOX");
        /* we need to open it READ_WRITE, because we want to move messages we already handled */
        try {
            folderInbox.open(Folder.READ_WRITE);
        } catch (FolderNotFoundException fnfe) {
            throw new Exception("Could not find INBOX folder: " + fnfe.getMessage(), fnfe);
        } catch (Exception e) {
            throw new Exception("Could not open INBOX folder: " + e.getMessage(), e);
        }

        /* here we have to split, because IMAP will be handled differently from POP3 */
        if (config.getProtocol().toLowerCase().equals("imap")) {
            /***
             * Open the default folder, under which will be the processed
             * and the invalid folder.
             ***/

            Folder folderDefault = null;
            try {
                folderDefault = store.getDefaultFolder();
            } catch (MessagingException me) {
                throw new Exception("Could not get default folder: " + me.getMessage(), me);
            }
            /* sanity check */
            try {
                if (!folderDefault.exists()) {
                    throw new Exception(
                            "Default folder does not exist. Cannot continue. This might indicate that this software does not like the given IMAP server. If you think you know what the problem is contact the author.");
                }
            } catch (MessagingException me) {
                throw new Exception("Could not test existence of the default folder: " + me.getMessage(), me);
            }

            /**
             * This is kind of a fallback mechanism. For some reasons it can happen that
             * the default folder has an empty name and exists() returns true, but when
             * trying to create a subfolder it generates an error message.
             * So what we do here is if the name of the default folder is empty, we
             * look for the "INBOX" folder, which has to exist and then create the
             * subfolders under this folder.
             */
            if (folderDefault.getName().equals("")) {
                this.log.warn("Default folder has empty name. Looking for 'INBOX' folder as root folder.");
                folderDefault = store.getFolder("INBOX");
                if (!folderDefault.exists()) {
                    throw new Exception(
                            "Could not find default folder and could not find 'INBOX' folder. Cannot continue. This might indicate that this software does not like the given IMAP server. If you think you know what the problem is contact the author.");
                }
            }

            /***
             * Open the folder for processed messages
             ***/

            /* get the folder where we store processed messages */
            Folder folderProcessed = folderDefault.getFolder("Processed");
            /* check if it exists */
            if (!folderProcessed.exists()) {
                /* does not exist, create it */
                try {
                    if (!folderProcessed.create(Folder.HOLDS_MESSAGES)) {
                        throw new Exception("Creating 'processed' folder failed.");
                    }
                } catch (MessagingException me) {
                    throw new Exception("Could not create 'processed' folder: " + me.getMessage(), me);
                }
            }
            /* we need to open it READ_WRITE, because we want to move messages we already handled to this folder */
            try {
                folderProcessed.open(Folder.READ_WRITE);
            } catch (FolderNotFoundException fnfe) {
                throw new Exception("Could not find 'processed' folder: " + fnfe.getMessage(), fnfe);
            } catch (Exception e) {
                throw new Exception("Could not open 'processed' folder: " + e.getMessage(), e);
            }

            /***
             * Open the folder for invalid messages
             ***/

            /* get the folder where we store invalid messages */
            Folder folderInvalid = folderDefault.getFolder("Invalid");
            /* check if it exists */
            if (!folderInvalid.exists()) {
                /* does not exist, create it */
                try {
                    if (!folderInvalid.create(Folder.HOLDS_MESSAGES)) {
                        throw new Exception("Creating 'invalid' folder failed.");
                    }
                } catch (MessagingException me) {
                    throw new Exception("Could not create 'invalid' folder: " + me.getMessage(), me);
                }
            }
            /* we need to open it READ_WRITE, because we want to move messages we already handled to this folder */
            try {
                folderInvalid.open(Folder.READ_WRITE);
            } catch (FolderNotFoundException fnfe) {
                throw new Exception("Could not find 'invalid' folder: " + fnfe.getMessage(), fnfe);
            } catch (Exception e) {
                throw new Exception("Could not open 'invalid' folder: " + e.getMessage(), e);
            }

            /***
             * Get all new messages
             ***/

            /* get all messages in the INBOX */
            Message message[] = folderInbox.getMessages();

            /* go through all messages and get the unseen ones (all should be unseen,
             * as the seen ones get moved to a different folder
             */
            for (int i = 0; i < message.length; i++) {

                if (message[i].isSet(Flags.Flag.SEEN)) {
                    /* this message has been seen, should not happen */
                    /* send email to the sender */
                    sendErrorMessage(message[i],
                            "This message has already been flagged as seen before being handled and was thus ignored.");
                    /* move this message to the invalid folder */
                    moveMessage(message[i], folderInbox, folderInvalid);
                    /* skip this message */
                    continue;
                }

                Space space = null;
                try {
                    space = getSpaceFromAddress(message[i]);
                } catch (Exception e) {
                    this.log.error("Could not get space from message: " + e.getMessage());
                    /* send email to the sender */
                    sendErrorMessage(message[i], "Could not get space from message: " + e.getMessage());
                    /* move this message to the invalid folder */
                    moveMessage(message[i], folderInbox, folderInvalid);
                    /* skip this message */
                    continue;
                }

                /* initialise content and attachments */
                blogEntryContent = null;
                attachments = new LinkedList();
                attachmentsInputStreams = new LinkedList();

                containsImage = false;

                /* get the content of this message */
                try {
                    Object content = message[i].getContent();
                    if (content instanceof Multipart) {
                        handleMultipart((Multipart) content);
                    } else {
                        handlePart(message[i]);
                    }
                } catch (Exception e) {
                    this.log.error("Error while getting content of message: " + e.getMessage(), e);
                    /* send email to the sender */
                    sendErrorMessage(message[i], "Error while getting content of message: " + e.getMessage());
                    /* move this message to the invalid folder */
                    moveMessage(message[i], folderInbox, folderInvalid);
                    /* skip this message */
                    continue;
                }

                try {
                    createBlogPost(space, message[i]);
                } catch (MessagingException me) {
                    this.log.error("Error while creating blog post: " + me.getMessage(), me);
                    /* send email to sender */
                    sendErrorMessage(message[i], "Error while creating blog post: " + me.getMessage());
                    /* move this message to the invalid folder */
                    moveMessage(message[i], folderInbox, folderInvalid);
                    /* skip this message */
                    continue;
                }

                /* move the message to the processed folder */
                moveMessage(message[i], folderInbox, folderProcessed);

            }

            /* close the folders, expunging deleted messages in the process */
            folderInbox.close(true);
            folderProcessed.close(true);
            folderInvalid.close(true);
            /* close the store */
            store.close();

        } else if (config.getProtocol().toLowerCase().equals("pop3")) {
            /* get all messages in this POP3 account */
            Message message[] = folderInbox.getMessages();

            /* go through all messages */
            for (int i = 0; i < message.length; i++) {

                Space space = null;
                try {
                    space = getSpaceFromAddress(message[i]);
                } catch (Exception e) {
                    this.log.error("Could not get space from message: " + e.getMessage());
                    /* send email to the sender */
                    sendErrorMessage(message[i], "Could not get space from message: " + e.getMessage());
                    /* delete this message */
                    message[i].setFlag(Flags.Flag.DELETED, true);
                    /* get the next message, this message will be deleted when
                     * closing the folder */
                    continue;
                }

                /* initialise content and attachments */
                blogEntryContent = null;
                attachments = new LinkedList();
                attachmentsInputStreams = new LinkedList();

                containsImage = false;

                /* get the content of this message */
                try {
                    Object content = message[i].getContent();
                    if (content instanceof Multipart) {
                        handleMultipart((Multipart) content);
                    } else {
                        handlePart(message[i]);
                    }
                } catch (Exception e) {
                    this.log.error("Error while getting content of message: " + e.getMessage(), e);
                    /* send email to the sender */
                    sendErrorMessage(message[i], "Error while getting content of message: " + e.getMessage());
                    /* delete this message */
                    message[i].setFlag(Flags.Flag.DELETED, true);
                    /* get the next message, this message will be deleted when
                     * closing the folder */
                    continue;
                }

                try {
                    createBlogPost(space, message[i]);
                } catch (MessagingException me) {
                    this.log.error("Error while creating blog post: " + me.getMessage(), me);
                    /* send email to the sender */
                    sendErrorMessage(message[i], "Error while creating blog post: " + me.getMessage());
                    /* delete this message */
                    message[i].setFlag(Flags.Flag.DELETED, true);
                    /* get the next message, this message will be deleted when
                     * closing the folder */
                    continue;
                }

                /* finished processing this message, delete it */
                message[i].setFlag(Flags.Flag.DELETED, true);
                /* get the next message, this message will be deleted when
                 * closing the folder */

            }

            /* close the pop3 folder, deleting all messages flagged as DELETED */
            folderInbox.close(true);
            /* close the mail store */
            store.close();

        } else {
            throw new Exception("Unknown protocol: " + config.getProtocol());
        }

    } catch (Exception e) {
        /* catch any exception which was not handled so far */
        this.log.error("Error while executing mail2news job: " + e.getMessage(), e);
        JobExecutionException jee = new JobExecutionException(
                "Error while executing mail2news job: " + e.getMessage(), e, false);
        throw jee;
    } finally {
        /* try to do some cleanup */
        try {
            store.close();
        } catch (Exception e) {
        }
    }
}

From source file:com.panet.imeta.job.entries.getpop.JobEntryGetPOP.java

@SuppressWarnings({ "unchecked" })
public Result execute(Result previousResult, int nr, Repository rep, Job parentJob) {
    LogWriter log = LogWriter.getInstance();
    Result result = previousResult;
    result.setResult(false);/*from   ww  w.  j a  v  a 2  s.  co  m*/
    result.setNrErrors(1);

    //Get system properties
    Properties prop = new Properties();
    prop.setProperty("mail.pop3s.rsetbeforequit", "true"); //$NON-NLS-1$ //$NON-NLS-2$
    prop.setProperty("mail.pop3.rsetbeforequit", "true"); //$NON-NLS-1$ //$NON-NLS-2$

    //Create session object
    Session sess = Session.getDefaultInstance(prop, null);
    sess.setDebug(true);

    FileObject fileObject = null;
    Store st = null;
    Folder f = null;
    try {
        int nbrmailtoretrieve = Const.toInt(firstmails, 0);
        String realOutputFolder = getRealOutputDirectory();
        fileObject = KettleVFS.getFileObject(realOutputFolder);

        // Check if output folder exists
        if (!fileObject.exists()) {
            log.logError(toString(),
                    Messages.getString("JobGetMailsFromPOP.FolderNotExists.Label", realOutputFolder)); //$NON-NLS-1$
        } else {
            if (fileObject.getType() == FileType.FOLDER) {
                String host = getRealServername();
                String user = getRealUsername();
                String pwd = getRealPassword();

                if (!getUseSSL()) {
                    //Create POP3 object
                    st = sess.getStore("pop3"); //$NON-NLS-1$

                    // Try to connect to the server
                    st.connect(host, user, pwd);
                } else {
                    // Ssupports POP3 connection with SSL, the connection is established via SSL.

                    String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; //$NON-NLS-1$
                    prop.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY); //$NON-NLS-1$
                    prop.setProperty("mail.pop3.socketFactory.fallback", "false"); //$NON-NLS-1$ //$NON-NLS-2$
                    prop.setProperty("mail.pop3.port", getRealSSLPort()); //$NON-NLS-1$
                    prop.setProperty("mail.pop3.socketFactory.port", getRealSSLPort()); //$NON-NLS-1$

                    URLName url = new URLName("pop3", host, Const.toInt(getRealSSLPort(), 995), "", user, pwd); //$NON-NLS-1$ //$NON-NLS-2$
                    st = new POP3SSLStore(sess, url);
                    st.connect();
                }
                if (log.isDetailed())
                    log.logDetailed(toString(),
                            Messages.getString("JobGetMailsFromPOP.LoggedWithUser.Label") + user); //$NON-NLS-1$

                //Open the INBOX FOLDER
                // For POP3, the only folder available is the INBOX.
                f = st.getFolder("INBOX"); //$NON-NLS-1$

                if (f == null) {
                    log.logError(toString(), Messages.getString("JobGetMailsFromPOP.InvalidFolder.Label")); //$NON-NLS-1$

                } else {
                    // Open folder
                    if (delete)
                        f.open(Folder.READ_WRITE);
                    else
                        f.open(Folder.READ_ONLY);

                    Message messageList[] = f.getMessages();
                    if (log.isDetailed()) {
                        log.logDetailed(toString(),
                                Messages.getString("JobGetMailsFromPOP.TotalMessagesFolder.Label", f.getName(), //$NON-NLS-1$
                                        String.valueOf(messageList.length)));
                        log.logDetailed(toString(),
                                Messages.getString("JobGetMailsFromPOP.TotalUnreadMessagesFolder.Label", //$NON-NLS-1$
                                        f.getName(), String.valueOf(f.getUnreadMessageCount())));
                    }
                    // Get emails
                    Message msg_list[] = getPOPMessages(f, retrievemails);

                    if (msg_list.length > 0) {
                        List<File> current_file_POP = new ArrayList<File>();
                        List<String> current_filepath_POP = new ArrayList<String>();
                        int nb_email_POP = 1;

                        String startpattern = "name"; //$NON-NLS-1$
                        if (!Const.isEmpty(getRealFilenamePattern())) {
                            startpattern = getRealFilenamePattern();
                        }

                        for (int i = 0; i < msg_list.length; i++) {
                            if ((nb_email_POP <= nbrmailtoretrieve && retrievemails == 2)
                                    || (retrievemails != 2)) {
                                Message msg_POP = msg_list[i];
                                if (log.isDetailed()) {
                                    log.logDetailed(toString(),
                                            Messages.getString("JobGetMailsFromPOP.EmailFrom.Label", //$NON-NLS-1$
                                                    msg_list[i].getFrom()[0].toString()));
                                    log.logDetailed(toString(), Messages.getString(
                                            "JobGetMailsFromPOP.EmailSubject.Label", msg_list[i].getSubject())); //$NON-NLS-1$
                                }
                                String localfilename_message = startpattern + "_" //$NON-NLS-1$
                                        + StringUtil.getFormattedDateTimeNow(true) + "_" + (i + 1) + ".mail"; //$NON-NLS-1$ //$NON-NLS-2$
                                if (log.isDetailed())
                                    log.logDetailed(toString(), Messages.getString(
                                            "JobGetMailsFromPOP.LocalFilename.Label", localfilename_message)); //$NON-NLS-1$

                                File filename_message = new File(realOutputFolder, localfilename_message);
                                OutputStream os_filename = new FileOutputStream(filename_message);
                                Enumeration<Header> enums_POP = msg_POP.getAllHeaders();
                                while (enums_POP.hasMoreElements())

                                {
                                    Header header_POP = enums_POP.nextElement();
                                    os_filename.write(new StringBuffer(header_POP.getName()).append(": ") //$NON-NLS-1$
                                            .append(header_POP.getValue()).append("\r\n").toString().getBytes()); //$NON-NLS-1$
                                }
                                os_filename.write("\r\n".getBytes()); //$NON-NLS-1$
                                InputStream in_POP = msg_POP.getInputStream();
                                byte[] buffer_POP = new byte[1024];
                                int length_POP = 0;
                                while ((length_POP = in_POP.read(buffer_POP, 0, 1024)) != -1) {
                                    os_filename.write(buffer_POP, 0, length_POP);

                                }
                                os_filename.close();
                                nb_email_POP++;
                                current_file_POP.add(filename_message);
                                current_filepath_POP.add(filename_message.getPath());

                                // Check attachments
                                Object content = msg_POP.getContent();
                                if (content instanceof Multipart) {
                                    handleMultipart(realOutputFolder, (Multipart) content);
                                }

                                // Check if mail has to be deleted
                                if (delete) {
                                    if (log.isDetailed())
                                        log.logDetailed(toString(),
                                                Messages.getString("JobGetMailsFromPOP.DeleteEmail.Label")); //$NON-NLS-1$
                                    msg_POP.setFlag(javax.mail.Flags.Flag.DELETED, true);
                                }
                            }
                        }
                    }

                    result.setNrErrors(0);
                    result.setResult(true);
                }

            } else {
                log.logError(toString(),
                        Messages.getString("JobGetMailsFromPOP.Error.NotAFolder", realOutputFolder));
            }
        }
    } catch (NoSuchProviderException e) {
        log.logError(toString(), Messages.getString("JobEntryGetPOP.ProviderException", e.getMessage())); //$NON-NLS-1$
    } catch (MessagingException e) {
        log.logError(toString(), Messages.getString("JobEntryGetPOP.MessagingException", e.getMessage())); //$NON-NLS-1$
    } catch (Exception e) {
        log.logError(toString(), Messages.getString("JobEntryGetPOP.GeneralException", e.getMessage())); //$NON-NLS-1$
    } finally {
        if (fileObject != null) {
            try {
                fileObject.close();
            } catch (IOException ex) {
            }
            ;
        }
        //close the folder, passing in a true value to expunge the deleted message
        try {
            if (f != null)
                f.close(true);
            if (st != null)
                st.close();
        } catch (Exception e) {
            log.logError(toString(), e.getMessage());
        }
        // free memory
        f = null;
        st = null;
        sess = null;
    }

    return result;
}

From source file:edu.stanford.muse.email.EmailFetcherStats.java

/**
 * best effort to prefetch messages for messages[startMsgIdx] onwards, up to
 * the IMAP_PREFETCH_BUFSIZE/*from   w  ww .j ava 2s  .c o  m*/
 * return List<String> if bodyTextOnly is true, otherwise List<MimeMessage>
 */
private List<?> do_imap_prefetch(Message[] messages, int startMsgIdx, Folder folder, boolean bodyTextOnly) {
    // its perfectly ok for correctness for this method to do nothing and return null
    List<?> prefetchedMessages = null;
    try {

        if (IMAP_PREFETCH_BUFSIZE > 0 && folder instanceof IMAPFolder) {
            int prefetch_messages_size = 0;

            int start_message_num = messages[startMsgIdx].getMessageNumber();
            int end_message_num = start_message_num;

            List<Integer> messageNums = new ArrayList<Integer>();

            // figure out message num range to fetch. if anything is unusual -- bad content type, non-consec. msg nums etc -- break out.
            // non consec. message numbers are a problem because they cause a very long imap command string, which we found was returning an "invalid command" response.
            int prev_message_num = -1;
            for (int msgIdx = startMsgIdx; msgIdx < messages.length; msgIdx++) {
                if (bodyTextOnly) {
                    String contentType = messages[msgIdx].getContentType().toLowerCase();
                    if (!contentType.startsWith("multipart/") && !contentType.startsWith("text/plain")) {
                        log.info("Warn: message idx" + msgIdx + " msg#" + messages[msgIdx].getMessageNumber()
                                + " has unexpected content type " + contentType);
                        break;
                    }
                }

                // check if sequence is as expected
                int next_message_num = messages[msgIdx].getMessageNumber(); // may be better to switch this to uid and prefetcher uses uid fetch
                if (next_message_num != prev_message_num + 1 && prev_message_num != -1)
                    break;

                // if this message would push prefetch size beyond the buf size, break out, not including this message
                if (prefetch_messages_size + messages[msgIdx].getSize() >= IMAP_PREFETCH_BUFSIZE)
                    break;
                prev_message_num = next_message_num;
                prefetch_messages_size += messages[msgIdx].getSize();
                messageNums.add(next_message_num);
            }

            if (messageNums.size() == 0)
                return null;

            // now we prefetch messages from start_message_num to end_message_num
            long startMillis = System.currentTimeMillis();
            log.info("prefetching " + messageNums.size() + " messages");
            ImapPrefetcher prefetcher = bodyTextOnly
                    ? new TextOnlyImapPrefetcher(((ImapPopEmailStore) emailStore).session, messageNums)
                    : new ImapPrefetcher(((ImapPopEmailStore) emailStore).session, messageNums);
            prefetchedMessages = (List<?>) ((IMAPFolder) folder).doCommand(prefetcher); // start_message_num, end_message_num));
            long elapsedMillis = System.currentTimeMillis() - startMillis;
            long kb_per_sec = prefetch_messages_size / elapsedMillis;
            log.info("prefetched " + messageNums.size() + " messages in " + Util.blur(folder.getName()) + " ["
                    + start_message_num + ":" + end_message_num + "], "
                    + Util.commatize(prefetch_messages_size / 1024) + "KB in " + Util.commatize(elapsedMillis)
                    + "ms (" + Util.commatize(kb_per_sec) + " KB/sec)");
        }
    } catch (Exception e) {
        Util.print_exception(e, log);
    }
    return prefetchedMessages;
}