Example usage for org.apache.commons.net.pop3 POP3Client sendCommand

List of usage examples for org.apache.commons.net.pop3 POP3Client sendCommand

Introduction

In this page you can find the example usage for org.apache.commons.net.pop3 POP3Client sendCommand.

Prototype

public int sendCommand(String command) throws IOException 

Source Link

Document

Sends a command with no arguments to the server and returns the reply code.

Usage

From source file:ProtocolRunner.java

private static void handlePOP3(
    POP3Client client, 
    ProtocolRunner runner, /* w w  w  .  j av a2s.  co m*/
    String commandString) 
    throws IOException {
        
    if(commandString == null) { // means just connected 
            return;            
    } else { // need to handle a command
        client.sendCommand(commandString);
    }
        
    runner.getTCPServerResponse().append(client.getReplyString());
}

From source file:org.apache.james.pop3server.POP3ServerTest.java

/**
 * Test for JAMES-1202 - This was failing before as the more then one connection to the same
 * mailbox was not handled the right way
 *//*from ww w  . j  a va 2s.  c  o m*/
@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);

}

From source file:org.apache.james.protocols.pop3.AbstractPOP3ServerTest.java

@Test
public void testAPop() throws Exception {
    InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort());

    ProtocolServer server = null;//ww w  .ja v  a 2  s.  com
    try {
        TestApopCmdHandler handler = new TestApopCmdHandler();
        server = createServer(createProtocol(handler), address);
        server.bind();

        POP3Client client = createClient();
        client.connect(address.getAddress().getHostAddress(), address.getPort());
        String welcomeMessage = client.getReplyString();

        // check for valid syntax that include all info needed for APOP
        assertThat(welcomeMessage.trim()).matches(Pattern.compile("\\+OK \\<-?\\d+\\.\\d+@.+\\> .+"));

        assertThat(client.sendCommand("APOP invalid invalid")).isEqualTo(POP3Reply.ERROR);

        handler.add("valid", new MockMailbox("id"));
        assertThat(client.sendCommand("APOP valid valid")).isEqualTo(POP3Reply.OK);

        assertThat(client.logout()).isTrue();

    } finally {
        if (server != null) {
            server.unbind();
        }
    }

}