messageAdded() method in javamail is not working

I have implemented the messageAdded method provided by Javamail API and my program do not enter into that method. Here is my code

public void addMessageListenerToFolder(Folder folder, EmailFolderBean<String> item) {
    System.out.println("Add message listener point 1");
    folder.addMessageCountListener(new MessageCountAdapter() {

        public void messageAdded(MessageCountEvent e) {
            for (int i = 0; i < e.getMessages().length; i++) {
                try {
                    System.out.println("Add message listener point 2");
                    Message currentMsg = folder.getMessage(folder.getMessageCount() - i);
                    item.addEmail(0, currentMsg);
                } catch (MessagingException ex) {
                    Logger.getLogger(FetchFolderService.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    });
}

I added two System.out statements, one at the second line of the above code and the other in the messageAdded() method the first statement print out every time this method is called but my program do not enter into messageAdded() and never print the second System.out statement. What could be the reason. Help me to resolve this issue.

Edit1: I have a service always running that send a folder.getMessageCount(); every 10 second. Edit2: I’m testing and its not the final I can increase the time later Here is my service.

@Override
    protected Task<Void> createTask() {
        return new Task<Void>() {
@Override
protected Task<Void> createTask() {
    return new Task<Void>() {

        @Override
        protected Void call() throws Exception {
            for (;;) {
                try {
                    Thread.sleep(10000);
                    if (FetchFolderService.noServiceActive()) {
                        System.out.println("Checking for Emails!!!!");
                        for (Folder folder : folderList) {
                            if (folder.getType() != Folder.HOLDS_FOLDERS && folder.isOpen()) {
                                folder.getMessageCount();

                            }
                        }
                    }
                } catch (InterruptedException | MessagingException e) {
                    e.printStackTrace();
                }
            }
        }
    };
}

And here is the debug info.

Deleting: C:UsersInzimamDocumentsNetBeansProjectsMail Clientbuildbuilt-jar.properties
deps-jar:
Updating property file: C:UsersInzimamDocumentsNetBeansProjectsMail Clientbuildbuilt-jar.properties
Compiling 1 source file to C:UsersInzimamDocumentsNetBeansProjectsMail Clientbuildclasses
compile-single:
Successful Authentication!
Folder: INBOX
Folder: [Gmail]
subFolder: All Mail
subFolder: Drafts
subFolder: Important
subFolder: Sent Mail
subFolder: Spam
subFolder: Starred
subFolder: Trash
Checking for Emails!!!!
Checking for Emails!!!!
Checking for Emails!!!!
Checking for Emails!!!!
Checking for Emails!!!!
Checking for Emails!!!!
Checking for Emails!!!!
debug-single:
BUILD SUCCESSFUL (total time: 1 minute 54 seconds)

Answer

There was a typo (error) in the code above. I used messageAdded() But javamail provide messagesAdded() method. So with the right spell and Override annotation everything is fine now.

Leave a Reply

Your email address will not be published. Required fields are marked *