javajakarta-mailpop3

addMessageCountListener doesn't work in javamail


I'm trying to use addMessageCountListener (for POP3 protocol) in a small project, but it doesn't work.

This is code where I added listener:

protected void openFolder() {
    try {

        this.inbox = this.store.getFolder("INBOX");

        if (inbox == null) {

            throw new Exception("No POP3 INBOX");

        }

        this.inbox.open(Folder.READ_WRITE);

        this.inbox.addMessageCountListener(new MessageCountAdapter() {

            public void messagesAdded(MessageCountEvent ev) {

                System.out.println("Event");

                Message [] msgs = ev.getMessages();

                for (Message msg : msgs) {

                    System.out.println("msg");

                    System.out.println(AbstractReceiverClient.getTextFromMsg("email", "subject", msg));
                }
            }

            public void messagesRemoved(MessageCountEvent ev) {}
       });
    }
    catch(Exception e) {
        System.out.println(e.getMessage());
    }
}

Here, I ask server for count of messages per 5 seconds.

public void worker() {
    try {
        while(true){
            java.lang.Thread.sleep(5000);
            System.out.println(this.inbox.getMessageCount());
        }
    }
    catch(Exception e){}
}

As the result, I just see only count of messages (g.e. 18), but I've sent a message after programm was run.

PS. Thanks for help


Solution

  • That's because the POP3 protocol doesn't allow new messages to appear in your INBOX while it's open.