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

The following examples show how to use javax.mail.Folder#getType() . 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 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 3
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 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: 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 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: 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 8
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 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: 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 11
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 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: 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 14
Source File: cfMailMessageData.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
public void getMessage( cfImapConnection imapConnection, String rootFolder, long messageID, String _attachURI, String _attachDIR ) throws cfmRunTimeException {
	try{
		Folder	folderToList;

		if ( rootFolder == null || rootFolder.length() == 0 )
			folderToList = imapConnection.mailStore.getDefaultFolder();
		else
			folderToList = imapConnection.mailStore.getFolder(rootFolder);
			
		if ( (folderToList.getType() & Folder.HOLDS_MESSAGES) != 0){
		
			if ( !folderToList.isOpen() )
				folderToList.open( Folder.READ_ONLY );

			boolean bResult = false;
			if ( folderToList instanceof UIDFolder )	
				bResult = extractMessage( ((UIDFolder)folderToList).getMessageByUID( messageID ), messageID, _attachURI, _attachDIR );
			else
				bResult = extractMessage( folderToList.getMessage( (int)messageID ), messageID, _attachURI, _attachDIR );
			
			if ( !bResult )	imapConnection.setStatus( false, "Message does not exist" );
			else imapConnection.setStatus( true, "" );
			
			folderToList.close(false);
		}

	}catch(Exception E){
		imapConnection.setStatus( false, E.getMessage() );
	}
}
 
Example 15
Source File: cfMailFolderMessagesData.java    From openbd-core with GNU General Public License v3.0 4 votes vote down vote up
public cfQueryResultData listFolderMessages( cfImapConnection imapConnection, String rootFolder, int startRow, int totalMessages, boolean reverseOrder ) {
	cfQueryResultData query = new cfQueryResultData( new String[]{"subject","id","rxddate","sentdate","from","to","cc","bcc","size","lines","answered","deleted","draft","flagged","recent","seen"}, "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_MESSAGES) != 0){
		
			if ( !folderToList.isOpen() )
				folderToList.open( Folder.READ_ONLY );
			
			Message[] messageArray;
			if ( startRow != -1 ){
				
				int folderCount = folderToList.getMessageCount();
				int start, end;
				if ( !reverseOrder ){
					start = startRow;
					if ( folderCount < (startRow+totalMessages-1) ){
						start = startRow;
						end = folderCount; 
					}else{
						end = startRow + totalMessages - 1;
					}
				}else{
					end = folderCount - startRow + 1;
					if ( folderCount < (startRow+totalMessages-1) ){ 
						start = 1;
					}else{
						start = folderCount - startRow - totalMessages + 2;
					}
					
				}
				
				messageArray = folderToList.getMessages( start, end );
				imapConnection.setTotalMessages( folderCount );
				
				
			}else{
				messageArray = folderToList.getMessages();
				imapConnection.setTotalMessages( messageArray.length );
			}

			// To improve performance, pre-fetch all of the message items
			// used by the CFIMAP list action. This will retrieve all of the
			// items for all of the messages with one single FETCH command.
			FetchProfile fp = new FetchProfile();
			fp.add(FetchProfile.Item.ENVELOPE);
			fp.add(FetchProfile.Item.FLAGS);
			fp.add(FetchProfile.Item.CONTENT_INFO);
			folderToList.fetch(messageArray, fp);
			List<Map<String, cfData>>	vectorMessages = new ArrayList<Map<String, cfData>>(messageArray.length);
			
			if ( reverseOrder ){
				int msgIndex = messageArray.length-1;
				for (int i = 0; i < messageArray.length; i++)
					vectorMessages.add( extractMessage( messageArray[msgIndex--] ) );
				
			}else{
				for (int i = 0; i < messageArray.length; i++)
					vectorMessages.add( extractMessage( messageArray[i] ) );
			}
			
			folderToList.close(false);
			query.populateQuery( vectorMessages );
		}

	}catch(Exception E){
		cfEngine.log( E.getMessage() );
		imapConnection.setStatus( false, E.getMessage() );
	}
	
	return query;
}
 
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;
}
 
Example 17
Source File: IMAPUtils.java    From elasticsearch-imap with Apache License 2.0 3 votes vote down vote up
public static void open(final Folder folder) throws MessagingException {

        if (folder != null && folder.exists() && !folder.isOpen() && (folder.getType() & Folder.HOLDS_MESSAGES) != 0) {
            folder.open(Folder.READ_ONLY);
        }

    }