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

The following examples show how to use javax.mail.Folder#create() . 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
/**
 * Set destination folder
 *
 * @param foldername   destination foldername
 * @param createFolder flag create folder if needed
 * @throws HopException
 */
public void setDestinationFolder( String foldername, boolean createFolder ) throws HopException {
  try {
    String[] folderparts = foldername.split( "/" );
    Folder f = this.getStore().getDefaultFolder();
    // Open destination folder
    for ( int i = 0; i < folderparts.length; i++ ) {
      f = f.getFolder( folderparts[ i ] );
      if ( !f.exists() ) {
        if ( createFolder ) {
          // Create folder
          f.create( Folder.HOLDS_MESSAGES );
        } else {
          throw new HopException( BaseMessages.getString( PKG, "MailConnection.Error.FolderNotFound", foldername ) );
        }
      }
    }
    this.destinationIMAPFolder = f;
  } catch ( Exception e ) {
    throw new HopException( e );
  }
}
 
Example 2
Source File: cfImapConnection.java    From openbd-core with GNU General Public License v3.0 6 votes vote down vote up
public void createFolder( String newFolder ){
	if ( newFolder.length() == 0 )
		return;
	
   try{
   
     Folder folderToUse = mailStore.getFolder( newFolder );
		
		if ( !folderToUse.exists() ){
			folderToUse.create( Folder.HOLDS_FOLDERS | Folder.HOLDS_MESSAGES );
		}
		
     setData( "succeeded", 	cfBooleanData.TRUE );
   
   } catch (Exception E ){
     setData( "errortext", 	new cfStringData( E.getMessage() ) );
     setData( "succeeded", 	cfBooleanData.FALSE );
   }
	
	setTotalMessages( 0 );
}
 
Example 3
Source File: MailConnection.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
/**
 * Set destination folder
 *
 * @param foldername
 *          destination foldername
 * @param createFolder
 *          flag create folder if needed
 * @throws KettleException
 */
public void setDestinationFolder( String foldername, boolean createFolder ) throws KettleException {
  try {
    String[] folderparts = foldername.split( "/" );
    Folder f = this.getStore().getDefaultFolder();
    // Open destination folder
    for ( int i = 0; i < folderparts.length; i++ ) {
      f = f.getFolder( folderparts[i] );
      if ( !f.exists() ) {
        if ( createFolder ) {
          // Create folder
          f.create( Folder.HOLDS_MESSAGES );
        } else {
          throw new KettleException( BaseMessages.getString( PKG, "MailConnection.Error.FolderNotFound", foldername ) );
        }
      }
    }
    this.destinationIMAPFolder = f;
  } catch ( Exception e ) {
    throw new KettleException( e );
  }
}
 
Example 4
Source File: EMailTestServer.java    From syndesis with Apache License 2.0 5 votes vote down vote up
public void generateFolder(String user, String password, String folderName) throws Exception {
    if (server instanceof SmtpServer) {
        throw new Exception("SMTP not applicable for generating folders");
    }

    Store store = server.createStore();
    store.connect(user, password);

    Folder newFolder = store.getFolder(folderName);
    if (! newFolder.exists()) {
        newFolder.create(Folder.HOLDS_MESSAGES);
        assertTrue(newFolder.exists());
    }

    newFolder.open(Folder.READ_WRITE);
    assertTrue(newFolder.isOpen());

    List<MimeMessage> msgs = new ArrayList<>();
    for (int i = 1; i <= 5; ++i) {
        // Use random content to avoid potential residual lingering problems
        String subject = folderName + SPACE + HYPHEN + SPACE + GreenMailUtil.random();
        String body = folderName + NEW_LINE + GreenMailUtil.random();
        GreenMailUser greenUser = greenMail.setUser(user, password);
        msgs.add(createTextMessage(greenUser.getEmail(), "Ben" + i + "@test.com", subject, body)); // Construct message
    }

    newFolder.appendMessages(msgs.toArray(new MimeMessage[0]));
    assertEquals(msgs.size(), newFolder.getMessageCount());
}
 
Example 5
Source File: IMAPTestCase.java    From javamail-mock2 with Apache License 2.0 4 votes vote down vote up
@Test
public void testRenameWithSubfolder() throws Exception {

    // final MockMailbox mb = MockMailbox.get("[email protected]");

    final MimeMessage msg = new MimeMessage((Session) null);
    msg.setSubject("Test");
    msg.setFrom("[email protected]");
    msg.setText("Some text here ...");
    msg.setRecipient(RecipientType.TO, new InternetAddress("[email protected]"));

    final Store store = session.getStore("mock_imap");
    store.connect("[email protected]", null);
    final Folder root = store.getDefaultFolder();
    final Folder level1 = root.getFolder("LEVEL1");
    level1.create(Folder.HOLDS_FOLDERS | Folder.HOLDS_MESSAGES);
    final Folder level2 = level1.getFolder("LEVEL2");
    level2.create(Folder.HOLDS_FOLDERS | Folder.HOLDS_MESSAGES);
    level1.appendMessages(new Message[] { msg, msg });
    level2.appendMessages(new Message[] { msg });

    Assert.assertTrue(level1.exists());
    Assert.assertEquals("LEVEL1", level1.getFullName());
    Assert.assertEquals("LEVEL1", level1.getName());
    Assert.assertEquals(2, level1.getMessageCount());

    Assert.assertTrue(level2.exists());
    Assert.assertEquals("LEVEL1/LEVEL2", level2.getFullName());
    Assert.assertEquals("LEVEL2", level2.getName());
    Assert.assertEquals(1, level2.getMessageCount());
    Assert.assertEquals(2, root.list().length);

    // getFolder creates a store
    level1.renameTo(store.getFolder("LEVEL-1R"));

    // TODO really need a create?
    Assert.assertTrue(!store.getFolder("LEVEL1").exists());

    Assert.assertTrue(level1.exists());
    Assert.assertEquals("LEVEL-1R", level1.getFullName());
    Assert.assertEquals("LEVEL-1R", level1.getName());
    Assert.assertEquals(2, level1.getMessageCount());

    Assert.assertTrue(level2.exists());
    Assert.assertEquals("LEVEL-1R/LEVEL2", level2.getFullName());
    Assert.assertEquals("LEVEL2", level2.getName());
    Assert.assertEquals(1, level2.getMessageCount());

    Assert.assertEquals(2, root.list().length);
}
 
Example 6
Source File: AbstractIMAPRiverUnitTest.java    From elasticsearch-imap with Apache License 2.0 4 votes vote down vote up
protected void createInitialIMAPTestdata(final Properties props, final String user, final String password, final int count,
        final boolean deleteall) throws MessagingException {
    final Session session = Session.getInstance(props);
    final Store store = session.getStore();
    store.connect(user, password);
    checkStoreForTestConnection(store);
    final Folder root = store.getDefaultFolder();
    final Folder testroot = root.getFolder("ES-IMAP-RIVER-TESTS");
    final Folder testrootl2 = testroot.getFolder("Level(2!");

    if (deleteall) {

        deleteMailsFromUserMailbox(props, "INBOX", 0, -1, user, password);

        if (testroot.exists()) {
            testroot.delete(true);
        }

        final Folder testrootenamed = root.getFolder("renamed_from_ES-IMAP-RIVER-TESTS");
        if (testrootenamed.exists()) {
            testrootenamed.delete(true);
        }

    }

    if (!testroot.exists()) {

        testroot.create(Folder.HOLDS_FOLDERS & Folder.HOLDS_MESSAGES);
        testroot.open(Folder.READ_WRITE);

        testrootl2.create(Folder.HOLDS_FOLDERS & Folder.HOLDS_MESSAGES);
        testrootl2.open(Folder.READ_WRITE);

    }

    final Folder inbox = root.getFolder("INBOX");
    inbox.open(Folder.READ_WRITE);

    final Message[] msgs = new Message[count];

    for (int i = 0; i < count; i++) {
        final MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(EMAIL_TO));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(EMAIL_USER_ADDRESS));
        message.setSubject(EMAIL_SUBJECT + "::" + i);
        message.setText(EMAIL_TEXT + "::" + SID++);
        message.setSentDate(new Date());
        msgs[i] = message;

    }

    inbox.appendMessages(msgs);
    testroot.appendMessages(msgs);
    testrootl2.appendMessages(msgs);

    IMAPUtils.close(inbox);
    IMAPUtils.close(testrootl2);
    IMAPUtils.close(testroot);
    IMAPUtils.close(store);

}