Java Code Examples for javax.mail.Folder#getFolder()
The following examples show how to use
javax.mail.Folder#getFolder() .
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 |
/** * 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: MailConnection.java From pentaho-kettle with Apache License 2.0 | 6 votes |
/** * 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 3
Source File: cfPOP3.java From openbd-core with GNU General Public License v3.0 | 5 votes |
private Folder openFolder( cfSession _Session, Store popStore ) throws cfmRunTimeException { try{ Folder folder = popStore.getDefaultFolder(); Folder popFolder = folder.getFolder("INBOX"); popFolder.open( Folder.READ_WRITE ); return popFolder; }catch(Exception E){ throw newRunTimeException( E.getMessage() ); } }
Example 4
Source File: MailConnection.java From hop with Apache License 2.0 | 5 votes |
private Folder getRecursiveFolder( String foldername ) throws MessagingException { Folder dfolder; String[] folderparts = foldername.split( "/" ); dfolder = this.getStore().getDefaultFolder(); // Open destination folder for ( int i = 0; i < folderparts.length; i++ ) { dfolder = dfolder.getFolder( folderparts[ i ] ); } return dfolder; }
Example 5
Source File: MailConnection.java From pentaho-kettle with Apache License 2.0 | 5 votes |
private Folder getRecursiveFolder( String foldername ) throws MessagingException { Folder dfolder; String[] folderparts = foldername.split( "/" ); dfolder = this.getStore().getDefaultFolder(); // Open destination folder for ( int i = 0; i < folderparts.length; i++ ) { dfolder = dfolder.getFolder( folderparts[i] ); } return dfolder; }
Example 6
Source File: IMAPTestCase.java From javamail-mock2 with Apache License 2.0 | 5 votes |
@Test(expected = MockTestException.class) public void testQUOTAUnsupported() throws Exception { final MockMailbox mb = MockMailbox.get("[email protected]"); final MailboxFolder mf = mb.getInbox(); 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]")); mf.add(msg); // 11 mf.add(msg); // 12 mf.add(msg); // 13 mb.getRoot().getOrAddSubFolder("test").create().add(msg); final Store store = session.getStore("mock_imap"); store.connect("[email protected]", null); final Folder defaultFolder = store.getDefaultFolder(); final Folder test = defaultFolder.getFolder("test"); final IMAPStore imapStore = (IMAPStore) store; try { imapStore.getQuota(""); } catch (final MessagingException e) { throw new MockTestException(e); } }
Example 7
Source File: IMAPTestCase.java From javamail-mock2 with Apache License 2.0 | 5 votes |
@Test public void testNotOnlyInbox() throws Exception { final MockMailbox mb = MockMailbox.get("[email protected]"); final MailboxFolder mf = mb.getInbox(); 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]")); mf.add(msg); // 11 mf.add(msg); // 12 mf.add(msg); // 13 mb.getRoot().getOrAddSubFolder("test").create().add(msg); final Store store = session.getStore("mock_imap"); store.connect("[email protected]", null); final Folder defaultFolder = store.getDefaultFolder(); final Folder test = defaultFolder.getFolder("test"); Assert.assertNotNull(test); final Folder inbox = defaultFolder.getFolder("INBOX"); Assert.assertNotNull(inbox); }
Example 8
Source File: IMAPTestCase.java From javamail-mock2 with Apache License 2.0 | 5 votes |
@Test public void testAppendMessage() throws Exception { final MockMailbox mb = MockMailbox.get("[email protected]"); final MailboxFolder mf = mb.getInbox(); 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]")); mf.add(msg); // 11 mf.add(msg); // 12 mf.add(msg); // 13 mb.getRoot().getOrAddSubFolder("test").create().add(msg); final Store store = session.getStore(Providers.getIMAPProvider("makes_no_difference_here", true, true)); store.connect("[email protected]", null); final Folder defaultFolder = store.getDefaultFolder(); final Folder inbox = defaultFolder.getFolder("INBOX"); inbox.open(Folder.READ_WRITE); inbox.appendMessages(new MimeMessage[] { msg }); Assert.assertEquals(4, inbox.getMessageCount()); inbox.close(true); }
Example 9
Source File: IMAPTestCase.java From javamail-mock2 with Apache License 2.0 | 5 votes |
@Test // (expected = MockTestException.class) public void testAppendFailMessage() throws Exception { final MockMailbox mb = MockMailbox.get("[email protected]"); final MailboxFolder mf = mb.getInbox(); 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]")); mf.add(msg); // 11 mf.add(msg); // 12 mf.add(msg); // 13 mb.getRoot().getOrAddSubFolder("test").create().add(msg); final Store store = session.getStore(); store.connect("[email protected]", null); final Folder defaultFolder = store.getDefaultFolder(); final Folder inbox = defaultFolder.getFolder("INBOX"); inbox.open(Folder.READ_ONLY); try { inbox.appendMessages(new MimeMessage[] { msg }); } catch (final IllegalStateException e) { // throw new MockTestException(e); } // Assert.fail("Exception expected before this point"); Assert.assertEquals(4, inbox.getMessageCount()); inbox.close(false); }
Example 10
Source File: IMAPTestCase.java From javamail-mock2 with Apache License 2.0 | 5 votes |
@Test(expected = MockTestException.class) public void testACLUnsupported() throws Exception { final MockMailbox mb = MockMailbox.get("[email protected]"); final MailboxFolder mf = mb.getInbox(); 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]")); mf.add(msg); // 11 mf.add(msg); // 12 mf.add(msg); // 13 mb.getRoot().getOrAddSubFolder("test").create().add(msg); final Store store = session.getStore("mock_imap"); store.connect("[email protected]", null); final Folder defaultFolder = store.getDefaultFolder(); final Folder test = defaultFolder.getFolder("test"); final IMAPFolder testImap = (IMAPFolder) test; try { testImap.getACL(); } catch (final MessagingException e) { throw new MockTestException(e); } }
Example 11
Source File: POP3TestCase.java From javamail-mock2 with Apache License 2.0 | 5 votes |
@Test(expected = MockTestException.class) public void testOnlyInbox() throws Exception { final MockMailbox mb = MockMailbox.get("[email protected]"); final MailboxFolder mf = mb.getInbox(); 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]")); mf.add(msg); // 11 mf.add(msg); // 12 mf.add(msg); // 13 mb.getRoot().getOrAddSubFolder("test").create().add(msg); final Store store = session.getStore(Providers.getPOP3Provider("makes_no_differernce", false, true)); store.connect("[email protected]", null); final Folder defaultFolder = store.getDefaultFolder(); try { defaultFolder.getFolder("test"); } catch (final MessagingException e) { throw new MockTestException(e); } }
Example 12
Source File: IMAPTestCase.java From javamail-mock2 with Apache License 2.0 | 4 votes |
@Test public void testDefaultFolder() throws Exception { final MockMailbox mb = MockMailbox.get("[email protected]"); final MailboxFolder mf = mb.getInbox(); 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]")); mf.add(msg); // 11 mf.add(msg); // 12 mf.add(msg); // 13 mb.getRoot().getOrAddSubFolder("test").create().add(msg); final Store store = session.getStore("mock_imaps"); store.connect("[email protected]", null); final Folder defaultFolder = store.getDefaultFolder(); final Folder inbox = defaultFolder.getFolder("INBOX"); inbox.open(Folder.READ_WRITE); Assert.assertEquals("[INBOX, test]", Arrays.toString(defaultFolder.list())); Assert.assertEquals(3, inbox.getMessageCount()); Assert.assertNotNull(inbox.getMessage(1)); inbox.close(true); Assert.assertEquals(3, inbox.getMessageCount()); inbox.open(Folder.READ_WRITE); inbox.getMessage(1).setFlag(Flag.DELETED, true); inbox.close(true); inbox.open(Folder.READ_WRITE); Assert.assertEquals(2, inbox.getMessageCount()); Assert.assertTrue(inbox instanceof UIDFolder); Assert.assertEquals(12L, ((UIDFolder) inbox).getUID(inbox.getMessage(1))); inbox.close(true); }
Example 13
Source File: IMAPTestCase.java From javamail-mock2 with Apache License 2.0 | 4 votes |
@Test public void testIDLESupported() throws Exception { final MockMailbox mb = MockMailbox.get("[email protected]"); final MailboxFolder mf = mb.getInbox(); 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]")); mf.add(msg); // 11 mf.add(msg); // 12 mf.add(msg); // 13 mb.getRoot().getOrAddSubFolder("test").create().add(msg); final Store store = session.getStore("mock_imap"); store.connect("[email protected]", null); final Folder defaultFolder = store.getDefaultFolder(); final IMAPFolder test = (IMAPFolder) defaultFolder.getFolder("test"); final IdleMessageCountListener mcl = new IdleMessageCountListener(); test.addMessageCountListener(mcl); test.open(Folder.READ_WRITE); final IdleThread it = new IdleThread(test); it.start(); test.addMessages(new Message[] { msg }); test.addMessages(new Message[] { msg }); test.addMessages(new Message[] { msg }); Thread.sleep(500); it.interrupt(); it.join(); test.close(true); Assert.assertNull(it.getException()); Assert.assertEquals(3, mcl.getAddedCount()); Assert.assertEquals(0, mcl.getRemovedCount()); Assert.assertEquals(4, test.getMessageCount()); }
Example 14
Source File: Main.java From javamail-mock2 with Apache License 2.0 | 4 votes |
public static void main(final String[] args) throws Exception { final Session session = Session.getInstance(getProperties()); final MockMailbox mb = MockMailbox.get("[email protected]"); final MailboxFolder mf = mb.getInbox(); 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]")); mf.add(msg); // 11 mf.add(msg); // 12 mf.add(msg); // 13 mb.getRoot().getOrAddSubFolder("test").create().add(msg); final Store store = session.getStore("mock_imap"); store.connect("[email protected]", null); final Folder defaultFolder = store.getDefaultFolder(); final IMAPFolder test = (IMAPFolder) defaultFolder.getFolder("test"); final IdleMessageCountListener mcl = new IdleMessageCountListener(); test.addMessageCountListener(mcl); test.open(Folder.READ_WRITE); final IdleThread it = new IdleThread(test); it.start(); final IdleThread it2 = new IdleThread(test); it2.start(); test.addMessages(new Message[] { msg }); test.addMessages(new Message[] { msg }); test.addMessages(new Message[] { msg }); // test.close(true); System.out.println(it.getException()); System.out.println(mcl.getAddedCount()); System.out.println(mcl.getRemovedCount()); System.out.println(test.getMessageCount()); System.out.println(it.getIdleCount()); // System.exit(1); }
Example 15
Source File: IMAPTestCase.java From javamail-mock2 with Apache License 2.0 | 4 votes |
@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 16
Source File: AbstractIMAPRiverUnitTest.java From elasticsearch-imap with Apache License 2.0 | 4 votes |
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); }
Example 17
Source File: MockImapClient.java From usergrid with Apache License 2.0 | 4 votes |
public Folder openMailFolder( Store store ) throws MessagingException { Folder folder = store.getDefaultFolder(); folder = folder.getFolder( "inbox" ); folder.open( Folder.READ_ONLY ); return folder; }
Example 18
Source File: POP3TestCase.java From javamail-mock2 with Apache License 2.0 | 4 votes |
@Test public void testDefaultFolder() throws Exception { final MockMailbox mb = MockMailbox.get("[email protected]"); final MailboxFolder mf = mb.getInbox(); 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]")); mf.add(msg); // 11 mf.add(msg); // 12 mf.add(msg); // 13 mb.getRoot().getOrAddSubFolder("test").create().add(msg); final Store store = session.getStore("mock_pop3"); store.connect("[email protected]", null); final Folder defaultFolder = store.getDefaultFolder(); final Folder inbox = defaultFolder.getFolder("INBOX"); inbox.open(Folder.READ_WRITE); Assert.assertEquals("[INBOX]", Arrays.toString(defaultFolder.list())); Assert.assertEquals(3, inbox.getMessageCount()); Assert.assertNotNull(inbox.getMessage(1)); inbox.close(true); Assert.assertEquals(3, inbox.getMessageCount()); inbox.open(Folder.READ_ONLY); inbox.getMessage(1).setFlag(Flag.DELETED, true); inbox.close(true); inbox.open(Folder.READ_WRITE); Assert.assertEquals(2, inbox.getMessageCount()); Assert.assertTrue(inbox instanceof POP3Folder); Assert.assertEquals("12", ((POP3Folder) inbox).getUID(inbox.getMessage(1))); inbox.close(true); }
Example 19
Source File: JavaMailContainer.java From scipio-erp with Apache License 2.0 | 4 votes |
protected void checkMessages(Store store, Session session) throws MessagingException { if (!store.isConnected()) { store.connect(); } // open the default folder Folder folder = store.getDefaultFolder(); if (!folder.exists()) { throw new MessagingException("No default (root) folder available"); } // open the inbox folder = folder.getFolder(INBOX); if (!folder.exists()) { throw new MessagingException("No INBOX folder available"); } // get the message count; stop if nothing to do folder.open(Folder.READ_WRITE); int totalMessages = folder.getMessageCount(); if (totalMessages == 0) { folder.close(false); return; } // get all messages Message[] messages = folder.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false)); FetchProfile profile = new FetchProfile(); profile.add(FetchProfile.Item.ENVELOPE); profile.add(FetchProfile.Item.FLAGS); profile.add("X-Mailer"); folder.fetch(messages, profile); // process each message for (Message message: messages) { // process each un-read message if (!message.isSet(Flags.Flag.SEEN)) { long messageSize = message.getSize(); if (message instanceof MimeMessage && messageSize >= maxSize) { Debug.logWarning("Message from: " + message.getFrom()[0] + "not received, too big, size:" + messageSize + " cannot be more than " + maxSize + " bytes", module); // set the message as read so it doesn't continue to try to process; but don't delete it message.setFlag(Flags.Flag.SEEN, true); } else { this.processMessage(message, session); if (Debug.verboseOn()) { Debug.logVerbose("Message from " + UtilMisc.toListArray(message.getFrom()) + " with subject [" + message.getSubject() + "] has been processed." , module); } message.setFlag(Flags.Flag.SEEN, true); if (Debug.verboseOn()) { Debug.logVerbose("Message [" + message.getSubject() + "] is marked seen", module); } // delete the message after processing if (deleteMail) { if (Debug.verboseOn()) { Debug.logVerbose("Message [" + message.getSubject() + "] is being deleted", module); } message.setFlag(Flags.Flag.DELETED, true); } } } } // expunge and close the folder folder.close(true); }