javax.mail.ReadOnlyFolderException Java Examples

The following examples show how to use javax.mail.ReadOnlyFolderException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StoreCopier.java    From mnIMAPSync with Apache License 2.0 6 votes vote down vote up
/**
 * Once the folder structure has been created it copies messages recursively from the root
 * folder.
 */
private void copySourceMessages(IMAPFolder sourceFolder) throws MessagingException {
    if (sourceFolder != null) {
        final String sourceFolderName = sourceFolder.getFullName();
        final String targetFolderName = sourceFolderNameToTarget(sourceFolderName, sourceIndex,
            targetIndex);
        if ((sourceFolder.getType() & Folder.HOLDS_MESSAGES) == Folder.HOLDS_MESSAGES) {
            //Manage Servers with public/read only folders.
            try {
                sourceFolder.open(Folder.READ_WRITE);
            } catch (ReadOnlyFolderException ex) {
                sourceFolder.open(Folder.READ_ONLY);
            }
            if (sourceFolder.getMode() != Folder.READ_ONLY) {
                sourceFolder.expunge();
            }
            ///////////////////////
            final int messageCount = sourceFolder.getMessageCount();
            sourceFolder.close(false);
            int pos = 1;
            while (pos + MNIMAPSync.BATCH_SIZE <= messageCount) {
                //Copy messages
                service.execute(new MessageCopier(this, sourceFolderName, targetFolderName, pos,
                        pos + MNIMAPSync.BATCH_SIZE, targetIndex.getFolderMessages(
                                targetFolderName)));
                pos = pos + MNIMAPSync.BATCH_SIZE;
            }
            service.execute(new MessageCopier(this, sourceFolderName, targetFolderName, pos,
                    messageCount,
                    targetIndex.getFolderMessages(targetFolderName)));
        }
        //Folder recursion. Get all children
        if ((sourceFolder.getType() & Folder.HOLDS_FOLDERS) == Folder.HOLDS_FOLDERS) {
            for (Folder child : sourceFolder.list()) {
                copySourceMessages((IMAPFolder) child);
            }
        }
    }
}