Package org.apache.commons.net.pop3

Examples of org.apache.commons.net.pop3.POP3Client


        Reader reader;
        POP3MessageInfo[] messages = null;

        MimeMessage mimeMsg = null;
        POP3Client pop3 = new POP3Client();
        // We want to timeout if a response takes longer than 60 seconds
        pop3.setDefaultTimeout(60000);

        for (int i = 0; i < 12; i++) {
            pop3.connect(pop3Host);

            if (!pop3.login(pop3User, pop3passwd)) {
                pop3.disconnect();
                AxisFault fault = new AxisFault("POP3", "( Could not login to server.  Check password. )", null, null);
                throw fault;
            }

            messages = pop3.listMessages();
            if (messages != null && messages.length > 0) {
                StringBuffer buffer = null;
                for (int j = 0; j < messages.length; j++) {
                    reader = pop3.retrieveMessage(messages[j].number);
                    if (reader == null) {
                        AxisFault fault = new AxisFault("POP3", "( Could not retrieve message header. )", null, null);
                        throw fault;
                    }

                    buffer = new StringBuffer();
                    BufferedReader bufferedReader = new BufferedReader(reader);
                    int ch;
                    while ((ch = bufferedReader.read()) != -1) {
                        buffer.append((char) ch);
                    }
                    bufferedReader.close();
                    if (buffer.toString().indexOf(id) != -1) {
                        ByteArrayInputStream bais = new ByteArrayInputStream(buffer.toString().getBytes());
                        Properties prop = new Properties();
                        Session session = Session.getDefaultInstance(prop, null);

                        mimeMsg = new MimeMessage(session, bais);
                        pop3.deleteMessage(messages[j].number);
                        break;
                    }
                    buffer = null;
                }
            }
            pop3.logout();
            pop3.disconnect();
            if (mimeMsg == null) {
                Thread.sleep(5000);
            } else {
                break;
            }
        }

        if (mimeMsg == null) {
            pop3.logout();
            pop3.disconnect();
            AxisFault fault = new AxisFault("POP3", "( Could not retrieve message list. )", null, null);
            throw fault;
        }

        String contentType = mimeMsg.getContentType();
View Full Code Here


    @Test
    public void testAuthenticationFail() throws Exception {
        finishSetUp(pop3Configuration);

        pop3Client = new POP3Client();
        pop3Client.connect("127.0.0.1", pop3Port);

        usersRepository.addUser("known", "test2");

        pop3Client.login("known", "test");
View Full Code Here

    @Test
    public void testUnknownUser() throws Exception {
        finishSetUp(pop3Configuration);

        pop3Client = new POP3Client();
        pop3Client.connect("127.0.0.1", pop3Port);

        pop3Client.login("unknown", "test");
        assertEquals(0, pop3Client.getState());
        assertTrue(pop3Client.getReplyString().startsWith("-ERR"));
View Full Code Here

    @Test
    public void testKnownUserEmptyInbox() throws Exception {
        finishSetUp(pop3Configuration);

        pop3Client = new POP3Client();
        pop3Client.connect("127.0.0.1", pop3Port);

        usersRepository.addUser("foo", "bar");

        // not authenticated
View Full Code Here

    @Test
    public void testUnknownCommand() throws Exception {
        finishSetUp(pop3Configuration);

        pop3Client = new POP3Client();
        pop3Client.connect("127.0.0.1", pop3Port);

        pop3Client.sendCommand("unkn");
        assertEquals(0, pop3Client.getState());
        assertEquals("Expected -ERR as result for an unknown command", pop3Client.getReplyString().substring(0, 4),
View Full Code Here

    public void testUidlCommand() throws Exception {
        finishSetUp(pop3Configuration);

        usersRepository.addUser("foo", "bar");

        pop3Client = new POP3Client();
        pop3Client.connect("127.0.0.1", pop3Port);

        pop3Client.sendCommand("uidl");
        assertEquals(0, pop3Client.getState());
View Full Code Here

    public void testMiscCommandsWithWithoutAuth() throws Exception {
        finishSetUp(pop3Configuration);

        usersRepository.addUser("foo", "bar");

        pop3Client = new POP3Client();
        pop3Client.connect("127.0.0.1", pop3Port);

        pop3Client.sendCommand("noop");
        assertEquals(0, pop3Client.getState());
        assertEquals("-ERR", pop3Client.getReplyString().substring(0, 4));
View Full Code Here

    @Test
    public void testKnownUserInboxWithMessages() throws Exception {
        finishSetUp(pop3Configuration);

        pop3Client = new POP3Client();
        pop3Client.connect("127.0.0.1", pop3Port);

        usersRepository.addUser("foo2", "bar2");

        MailboxPath mailboxPath = new MailboxPath(MailboxConstants.USER_NAMESPACE, "foo2", "INBOX");
View Full Code Here

     */
    @Test
    public void testStatUidlList() throws Exception {
        finishSetUp(pop3Configuration);

        pop3Client = new POP3Client();
        pop3Client.connect("127.0.0.1", pop3Port);

        usersRepository.addUser("foo2", "bar2");

        MailboxPath mailboxPath = new MailboxPath(MailboxConstants.USER_NAMESPACE, "foo2", "INBOX");
View Full Code Here

    @Test
    @Ignore
    public void testStatUidlListTwoConnections() throws Exception {
        finishSetUp(pop3Configuration);

        pop3Client = new POP3Client();
        pop3Client.connect("127.0.0.1", pop3Port);

        usersRepository.addUser("foo2", "bar2");

        MailboxPath mailboxPath = new MailboxPath(MailboxConstants.USER_NAMESPACE, "foo2", "INBOX");
        MailboxSession session = mailboxManager.login("foo2", "bar2", LoggerFactory.getLogger("Test"));

        if (!mailboxManager.mailboxExists(mailboxPath, session)) {
            mailboxManager.createMailbox(mailboxPath, session);
        }

        int msgCount = 100;
        for (int i = 0; i < msgCount; i++) {
            mailboxManager.getMailbox(mailboxPath, session).appendMessage(new ByteArrayInputStream(("Subject: test\r\n\r\n" + i).
                    getBytes()), new Date(), session, true, new Flags());
        }

        pop3Client.login("foo2", "bar2");
        assertEquals(1, pop3Client.getState());

        POP3MessageInfo[] listEntries = pop3Client.listMessages();
        POP3MessageInfo[] uidlEntries = pop3Client.listUniqueIdentifiers();
        POP3MessageInfo statInfo = pop3Client.status();
        assertEquals(msgCount, listEntries.length);
        assertEquals(msgCount, uidlEntries.length);
        assertEquals(msgCount, statInfo.number);

        POP3Client m_pop3Protocol2 = new POP3Client();
        m_pop3Protocol2.connect("127.0.0.1", pop3Port);
        m_pop3Protocol2.login("foo2", "bar2");
        assertEquals(1, m_pop3Protocol2.getState());

        POP3MessageInfo[] listEntries2 = m_pop3Protocol2.listMessages();
        POP3MessageInfo[] uidlEntries2 = m_pop3Protocol2.listUniqueIdentifiers();
        POP3MessageInfo statInfo2 = m_pop3Protocol2.status();
        assertEquals(msgCount, listEntries2.length);
        assertEquals(msgCount, uidlEntries2.length);
        assertEquals(msgCount, statInfo2.number);

        pop3Client.deleteMessage(1);
        listEntries = pop3Client.listMessages();
        uidlEntries = pop3Client.listUniqueIdentifiers();
        statInfo = pop3Client.status();
        assertEquals(msgCount - 1, listEntries.length);
        assertEquals(msgCount - 1, uidlEntries.length);
        assertEquals(msgCount - 1, statInfo.number);

        // even after the message was deleted it should get displayed in the
        // second connection
        listEntries2 = m_pop3Protocol2.listMessages();
        uidlEntries2 = m_pop3Protocol2.listUniqueIdentifiers();
        statInfo2 = m_pop3Protocol2.status();
        assertEquals(msgCount, listEntries2.length);
        assertEquals(msgCount, uidlEntries2.length);
        assertEquals(msgCount, statInfo2.number);

        assertTrue(pop3Client.logout());
        pop3Client.disconnect();

        // even after the message was deleted and the session was quit it should
        // get displayed in the second connection
        listEntries2 = m_pop3Protocol2.listMessages();
        uidlEntries2 = m_pop3Protocol2.listUniqueIdentifiers();
        statInfo2 = m_pop3Protocol2.status();
        assertEquals(msgCount, listEntries2.length);
        assertEquals(msgCount, uidlEntries2.length);
        assertEquals(msgCount, statInfo2.number);

        // This both should error and so return null
        assertNull(m_pop3Protocol2.retrieveMessageTop(1, 100));
        assertNull(m_pop3Protocol2.retrieveMessage(1));

        m_pop3Protocol2.sendCommand("quit");
        m_pop3Protocol2.disconnect();

        mailboxManager.deleteMailbox(mailboxPath, session);
       
    }
View Full Code Here

TOP

Related Classes of org.apache.commons.net.pop3.POP3Client

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.