Java Code Examples for javax.mail.Folder#list()

The following examples show how to use javax.mail.Folder#list() . 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: MailConnection.java    From hop with Apache License 2.0 6 votes vote down vote up
private HashSet<String> returnSubfolders( Folder folder ) throws HopException {
  HashSet<String> list = new HashSet<String>();
  try {
    if ( ( folder.getType() & Folder.HOLDS_FOLDERS ) != 0 ) {
      Folder[] f = folder.list();
      for ( int i = 0; i < f.length; i++ ) {
        // Search for sub folders
        if ( ( f[ i ].getType() & Folder.HOLDS_FOLDERS ) != 0 ) {
          list.add( f[ i ].getFullName() );
          list.addAll( returnSubfolders( f[ i ] ) );
        }
      }
    }
  } catch ( MessagingException m ) {
    throw new HopException( m );
  }
  return list;
}
 
Example 2
Source File: MailListener.java    From Ardulink-1 with Apache License 2.0 6 votes vote down vote up
private static void printFolders(Folder folder) throws MessagingException {
		System.out.println(folder.getFullName() + " - " + folder.getName());
		Folder[] childs = folder.list();
		System.out.println("************childs***************");
		for (int i = 0; i < childs.length; i++) {
			printFolders(childs[i]);
		}
		

//		Folder[] personalNamespaces = store.getPersonalNamespaces();
//		System.out.println("************personalNamespaces***************");
//		for (int i = 0; i < personalNamespaces.length; i++) {
//			System.out.println(personalNamespaces[i].getFullName() + " - " + personalNamespaces[i].getName());
//		}
//		
//		Folder[] sharedNamespaces = store.getSharedNamespaces();
//		System.out.println("************sharedNamespaces***************");
//		for (int i = 0; i < sharedNamespaces.length; i++) {
//			System.out.println(sharedNamespaces[i].getFullName() + " - " + sharedNamespaces[i].getName());
//		}
	}
 
Example 3
Source File: MailListener.java    From Ardulink-1 with Apache License 2.0 6 votes vote down vote up
private static void clear(Folder folder) throws MessagingException {
	if(folder.exists() && (folder.getType() & Folder.HOLDS_MESSAGES) == Folder.HOLDS_MESSAGES) {
		int totMessages = folder.getMessageCount();
		System.out.println(totMessages + " messages from folder: " + folder.getFullName());
		folder.open(Folder.READ_WRITE);
		Message[] messages = folder.getMessages();
		for (int i = 0; i < messages.length; i++) {
			System.out.println("Deleting message: " + (i+1) + " of " + totMessages);
			messages[i].setFlag(Flags.Flag.DELETED, true);
		}
		folder.close(true);
	}
	Folder[] childs = folder.list();
	for (int i = 0; i < childs.length; i++) {
		clear(childs[i]);
	}
}
 
Example 4
Source File: cfMailFolderData.java    From openbd-core with GNU General Public License v3.0 6 votes vote down vote up
public cfQueryResultData listAllFolders( cfImapConnection imapConnection) throws cfmRunTimeException {
	cfQueryResultData query	= new cfQueryResultData( new String[]{"fullname","name","totalmessages","unread","new"},"CFIMAP" );						
	
	try{
		Folder	folderToList;
		folderToList = imapConnection.mailStore.getDefaultFolder();

		if ( (folderToList.getType() & Folder.HOLDS_FOLDERS) != 0){
			Folder[] folderArray = folderToList.list("*");
			query.addRow( folderArray.length );				
			
			for (int i = 0; i < folderArray.length; i++)
				folderRow( query, i+1, folderArray[i] );
		}

	}catch(Exception E){}
	
	return query;
}
 
Example 5
Source File: JavamailService.java    From lemon with Apache License 2.0 6 votes vote down vote up
public void receiveByFolder(Folder folder, JavamailConfig javamailConfig)
        throws MessagingException, IOException {
    logger.info("receive : {}", folder);

    if ((Folder.HOLDS_MESSAGES & folder.getType()) != 0) {
        this.receiveMessageByFolder(folder, javamailConfig);
    }

    if ((Folder.HOLDS_FOLDERS & folder.getType()) != 0) {
        for (Folder childFolder : folder.list()) {
            this.receiveByFolder(childFolder, javamailConfig);
        }
    }

    if (folder.isOpen()) {
        // 关闭资源
        folder.close(false);
    }
}
 
Example 6
Source File: StoreDeleter.java    From mnIMAPSync with Apache License 2.0 6 votes vote down vote up
private void deleteTargetFolder(Folder folder) throws MessagingException {
    final String targetFolderName = folder.getFullName();
    final String sourceFolderName = targetToSourceFolderName(targetFolderName, sourceIndex, targetIndex);
    //Delete folder
    if (!sourceIndex.containsFolder(sourceFolderName)) {
        //Delete recursively
        targetStore.getFolder(targetFolderName).delete(true);
        incrementFoldersDeletedCount();
    }
    //Folder recursion. Get all children
    if ((folder.getType() & Folder.HOLDS_FOLDERS) == Folder.HOLDS_FOLDERS) {
        for (Folder child : folder.list()) {
            deleteTargetFolder(child);
        }
    }
}
 
Example 7
Source File: StoreCopier.java    From mnIMAPSync with Apache License 2.0 6 votes vote down vote up
/**
 * Create folders in the target server recursively from the source.
 *
 * It also indexes the source store folders if we want to delete target folders that no longer
 * exist
 */
private void copySourceFolder(Folder folder) throws MessagingException {
    final String sourceFolderName = folder.getFullName();
    final String targetFolderName = sourceFolderNameToTarget(sourceFolderName, sourceIndex,
        targetIndex);
    //Index for delete after copy (if necessary)
    if (sourceIndex != null) {
        sourceIndex.addFolder(sourceFolderName);
    }
    //Copy folder
    if (!targetIndex.containsFolder(targetFolderName)) {
        if (!targetStore.getFolder(targetFolderName).create(folder.getType())) {
            throw new MessagingException(String.format(
                    "Couldn't create folder: %s in target server.", sourceFolderName));
        }
        incrementFoldersCopiedCount();
    } else {
        incrementFoldersSkippedCount();
    }
    //Folder recursion. Get all children
    if ((folder.getType() & Folder.HOLDS_FOLDERS) == Folder.HOLDS_FOLDERS) {
        for (Folder child : folder.list()) {
            copySourceFolder(child);
        }
    }
}
 
Example 8
Source File: MailConnection.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
private HashSet<String> returnSubfolders( Folder folder ) throws KettleException {
  HashSet<String> list = new HashSet<String>();
  try {
    if ( ( folder.getType() & Folder.HOLDS_FOLDERS ) != 0 ) {
      Folder[] f = folder.list();
      for ( int i = 0; i < f.length; i++ ) {
        // Search for sub folders
        if ( ( f[i].getType() & Folder.HOLDS_FOLDERS ) != 0 ) {
          list.add( f[i].getFullName() );
          list.addAll( returnSubfolders( f[i] ) );
        }
      }
    }
  } catch ( MessagingException m ) {
    throw new KettleException( m );
  }
  return list;
}
 
Example 9
Source File: SelectFolderDialog.java    From hop with Apache License 2.0 5 votes vote down vote up
private void buildFoldersTree( Folder folder, TreeItem parentTreeItem, boolean topfolder ) throws MessagingException {
  if ( ( folder.getType() & Folder.HOLDS_FOLDERS ) != 0 ) {
    Folder[] f = folder.list();
    for ( int i = 0; i < f.length; i++ ) {
      tiTree = topfolder ? new TreeItem( wTree, SWT.NONE ) : new TreeItem( parentTreeItem, SWT.NONE );
      tiTree.setImage( guiresource.getImageBol() );
      tiTree.setText( f[ i ].getName() );
      // Search for sub folders
      if ( ( f[ i ].getType() & Folder.HOLDS_FOLDERS ) != 0 ) {
        buildFoldersTree( f[ i ], tiTree, false );
      }
    }
  }
}
 
Example 10
Source File: MailListener.java    From Ardulink-1 with Apache License 2.0 5 votes vote down vote up
private static void clearMainFolder(Folder folder) throws MessagingException {
	System.out.println("Deleting old messages...");
	Folder[] childs = folder.list();
	for (int i = 0; i < childs.length; i++) {
		clear(childs[i]);
	}
	clear(folder);
	System.out.println("Messages deleted.");
}
 
Example 11
Source File: ParallelPollingIMAPMailSource.java    From elasticsearch-imap with Apache License 2.0 5 votes vote down vote up
protected void recurseFolders(final Folder folder, final Pattern pattern) throws MessagingException, IOException {

        if (folder != null) {

            if (es == null || es.isShutdown() || es.isTerminated() || Thread.currentThread().isInterrupted()) {

                logger.warn("Stop processing of mails due to mail source is closed");
                return;

            }

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

                if (pattern != null && !pattern.matcher(folder.getFullName()).matches()) {
                    logger.debug("Pattern {} does not match {}", pattern.pattern(), folder.getFullName());
                    return;
                }
                IMAPUtils.open(folder);

                try {
                    fetch(folder);
                } finally {
                    IMAPUtils.close(folder);
                    logger.debug("fetch {} done", folder.getFullName());
                }
            }

            if ((folder.getType() & Folder.HOLDS_FOLDERS) != 0) {
                for (final Folder subfolder : folder.list()) {

                    recurseFolders(subfolder, pattern);

                }
            }

        }

    }
 
Example 12
Source File: ParallelPollingPOPMailSource.java    From elasticsearch-imap with Apache License 2.0 5 votes vote down vote up
protected void recurseFolders(final Folder folder, final Pattern pattern) throws MessagingException, IOException {

        if (folder != null) {

            if (es == null || es.isShutdown() || es.isTerminated() || Thread.currentThread().isInterrupted()) {

                logger.warn("Stop processing of mails due to mail source is closed");
                return;

            }

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

                if (pattern != null && !pattern.matcher(folder.getFullName()).matches()) {
                    logger.debug("Pattern {} does not match {}", pattern.pattern(), folder.getFullName());
                    return;
                }
                IMAPUtils.open(folder);
                try {
                    fetch(folder);
                } finally {
                    IMAPUtils.close(folder);
                }
            }

            if ((folder.getType() & Folder.HOLDS_FOLDERS) != 0) {
                for (final Folder subfolder : folder.list()) {

                    recurseFolders(subfolder, pattern);

                }
            }

        }

    }
 
Example 13
Source File: StoreCrawler.java    From mnIMAPSync with Apache License 2.0 5 votes vote down vote up
private static void crawlFolders(Store store, Index index, Folder folder,
    ExecutorService service) throws MessagingException {
  if (folder != null) {
    final String folderName = folder.getFullName();
    index.addFolder(folderName);
    if ((folder.getType() & Folder.HOLDS_MESSAGES) == Folder.HOLDS_MESSAGES) {
      folder.open(Folder.READ_ONLY);
      if (folder.getMode() != Folder.READ_ONLY) {
        folder.expunge();
      }
      final int messageCount = folder.getMessageCount();
      folder.close(false);
      int pos = 1;
      while (pos + MNIMAPSync.BATCH_SIZE <= messageCount) {
        service.execute(new FolderCrawler(store, folderName, pos,
            pos + MNIMAPSync.BATCH_SIZE, index));
        pos = pos + MNIMAPSync.BATCH_SIZE;
      }
      service.execute(new FolderCrawler(store, folderName, pos, messageCount, index));
    }
    //Folder recursion. Get all children
    if ((folder.getType() & Folder.HOLDS_FOLDERS) == Folder.HOLDS_FOLDERS) {
      for (Folder child : folder.list()) {
        crawlFolders(store, index, child, service);
      }
    }
  }
}
 
Example 14
Source File: StoreDeleter.java    From mnIMAPSync with Apache License 2.0 5 votes vote down vote up
private void deleteTargetMessages(Folder targetFolder) throws MessagingException {
    if (targetFolder != null) {
        final String targetFolderName = targetFolder.getFullName();
        final String sourceFolderName = targetToSourceFolderName(targetFolderName, sourceIndex, targetIndex);
        if ((targetFolder.getType() & Folder.HOLDS_MESSAGES) == Folder.HOLDS_MESSAGES) {
            targetFolder.open(Folder.READ_WRITE);
            if (targetFolder.getMode() != Folder.READ_ONLY) {
                targetFolder.expunge();
            }
            final int messageCount = targetFolder.getMessageCount();
            targetFolder.close(false);
            int pos = 1;
            while (pos + MNIMAPSync.BATCH_SIZE <= messageCount) {
                service.execute(
                        new MessageDeleter(this, targetFolderName, pos,
                                pos + MNIMAPSync.BATCH_SIZE, false, sourceIndex.
                                getFolderMessages(sourceFolderName)));
                pos = pos + MNIMAPSync.BATCH_SIZE;
            }
            service.execute(new MessageDeleter(this, targetFolderName,
                    pos, messageCount, true, sourceIndex.getFolderMessages(sourceFolderName)));
        }
        //Folder recursion. Get all children
        if ((targetFolder.getType() & Folder.HOLDS_FOLDERS) == Folder.HOLDS_FOLDERS) {
            for (Folder child : targetFolder.list()) {
                deleteTargetMessages(child);
            }
        }
    }
}
 
Example 15
Source File: SelectFolderDialog.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private void buildFoldersTree( Folder folder, TreeItem parentTreeItem, boolean topfolder ) throws MessagingException {
  if ( ( folder.getType() & Folder.HOLDS_FOLDERS ) != 0 ) {
    Folder[] f = folder.list();
    for ( int i = 0; i < f.length; i++ ) {
      tiTree = topfolder ? new TreeItem( wTree, SWT.NONE ) : new TreeItem( parentTreeItem, SWT.NONE );
      tiTree.setImage( guiresource.getImageBol() );
      tiTree.setText( f[i].getName() );
      // Search for sub folders
      if ( ( f[i].getType() & Folder.HOLDS_FOLDERS ) != 0 ) {
        buildFoldersTree( f[i], tiTree, false );
      }
    }
  }
}
 
Example 16
Source File: cfMailFolderData.java    From openbd-core with GNU General Public License v3.0 4 votes vote down vote up
public cfQueryResultData listFolder( cfImapConnection imapConnection, String rootFolder) throws cfmRunTimeException {
	
	cfQueryResultData query	= new cfQueryResultData( new String[]{"fullname","name","totalmessages","unread","new"},"CFIMAP" );						

	try{
	
		Folder	folderToList;

		if ( rootFolder == null || rootFolder.length() == 0 )
			folderToList = imapConnection.mailStore.getDefaultFolder();
		else
			folderToList = imapConnection.mailStore.getFolder(rootFolder);
			
		if ( (folderToList.getType() & Folder.HOLDS_FOLDERS) != 0){
			Folder[] folderArray = folderToList.list();
			
			query.addRow( folderArray.length );				
			
			for (int i = 0; i < folderArray.length; i++)
				folderRow( query, i+1, folderArray[i] );
		}

	}catch(Exception E){}
	
	return query;
}