Java Code Examples for javax.mail.Store#getDefaultFolder()

The following examples show how to use javax.mail.Store#getDefaultFolder() . 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: ThunderbirdMailbox.java    From mail-importer with Apache License 2.0 6 votes vote down vote up
public JavaxMailStorage get() throws MessagingException {
  Properties properties = new Properties();
  properties.setProperty("mail.store.protocol", "mstor");
  properties.setProperty("mstor.mbox.metadataStrategy", "none");
  properties.setProperty("mstor.mbox.cacheBuffers", "disabled");
  properties.setProperty("mstor.mbox.bufferStrategy", "mapped");
  properties.setProperty("mstor.metadata", "disabled");
  properties.setProperty("mstor.mozillaCompatibility", "enabled");

  Session session = Session.getDefaultInstance(properties);

  // /Users/flan/Desktop/Copy of Marie's Mail/Mail/Mail/mail.lean.to
  File mailbox = new File(commandLineArguments.mailboxFileName);
  if (!mailbox.exists()) {
    throw new MessagingException("No such mailbox:" + mailbox);
  }

  Store store = session.getStore(
      new URLName("mstor:" + mailbox.getAbsolutePath()));
  store.connect();

  return new ThunderbirdMailStorage(
      logger,
      new JavaxMailFolder(store.getDefaultFolder()),
      statusParser);
}
 
Example 2
Source File: IMAPTestCase.java    From javamail-mock2 with Apache License 2.0 5 votes vote down vote up
@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 3
Source File: IMAPTestCase.java    From javamail-mock2 with Apache License 2.0 5 votes vote down vote up
@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 4
Source File: IMAPTestCase.java    From javamail-mock2 with Apache License 2.0 5 votes vote down vote up
@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 5
Source File: IMAPTestCase.java    From javamail-mock2 with Apache License 2.0 5 votes vote down vote up
@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 6
Source File: IMAPTestCase.java    From javamail-mock2 with Apache License 2.0 5 votes vote down vote up
@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 7
Source File: POP3TestCase.java    From javamail-mock2 with Apache License 2.0 5 votes vote down vote up
@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 8
Source File: cfPOP3.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
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 9
Source File: Main.java    From javamail-mock2 with Apache License 2.0 4 votes vote down vote up
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 10
Source File: JavaMailContainer.java    From scipio-erp with Apache License 2.0 4 votes vote down vote up
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);
}
 
Example 11
Source File: IMAPTestCase.java    From javamail-mock2 with Apache License 2.0 4 votes vote down vote up
@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 12
Source File: IMAPTestCase.java    From javamail-mock2 with Apache License 2.0 4 votes vote down vote up
@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 13
Source File: POP3TestCase.java    From javamail-mock2 with Apache License 2.0 4 votes vote down vote up
@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 14
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 15
Source File: ParallelPollingPOPMailSource.java    From elasticsearch-imap with Apache License 2.0 4 votes vote down vote up
protected void fetch(final Pattern unusedPattern, final String unusedFolderName) throws MessagingException, IOException {

        logger.debug("fetch() - folderName: {}", "INBOX");

        final Store store = Session.getInstance(props).getStore();
        store.connect(user, password);

        final Folder folder = store.getDefaultFolder();
        try {

            if (!folder.exists()) {
                logger.error("Folder {} does not exist on the server", folder.getFullName());
                return;
            }

            IMAPUtils.open(folder);

            recurseFolders(folder, null);

        } finally {

            IMAPUtils.close(folder);
            IMAPUtils.close(store);
        }
    }
 
Example 16
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);

}
 
Example 17
Source File: MockImapClient.java    From usergrid with Apache License 2.0 4 votes vote down vote up
public Folder openMailFolder( Store store ) throws MessagingException {
    Folder folder = store.getDefaultFolder();
    folder = folder.getFolder( "inbox" );
    folder.open( Folder.READ_ONLY );
    return folder;
}