Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IMAP messageChangedListener() is not getting fired

Tags:

jakarta-mail

I'm implementing a IMAP client using java mail API. What I want is to be notified about mail count and the changes (Read/Unread etc) done to the mail. For this, I have wrote two listeners for the opened IMAP Folder as follows:

IMAPStore imapStore = (IMAPStore) session.getStore("imaps");
        imapStore.connect();

final IMAPFolder folder = (IMAPFolder) imapStore.getFolder("Inbox");
        folder.open(IMAPFolder.READ_WRITE);

folder.addMessageCountListener(new MessageCountListener() {

            public void messagesAdded(MessageCountEvent e) {
                System.out.println("Message Count Event Fired");
            }

            public void messagesRemoved(MessageCountEvent e) {
                System.out.println("Message Removed Event fired");
            }
        });

folder.addMessageChangedListener(new MessageChangedListener() {

            public void messageChanged(MessageChangedEvent e) {
                System.out.println("Message Changed Event fired");
            }
        });

And I'm sending the IMAP IDLE command to the server in a separate thread as follows.

Thread t = new Thread(new Runnable() {

            public void run() {
                try {
                    while (true) {
                        folder.idle();
                    }
                } catch (MessagingException ex) {
                    //Handling exception goes here
                }
            }
        });

        t.start();

But the problem is "MessageCountListener" is getting fired whenever new mail comes or a mail deleted. But when a mail changed happened, the "MessageChangedListener" is not getting fired.

like image 970
Priyan Perera Avatar asked Nov 13 '12 04:11

Priyan Perera


3 Answers

JavaMail depends on the server to send the notification. The IMAP spec allows a fair amount of flexibility in when, or if, servers send notifications. Not to mention that some servers don't fully comply with the IMAP spec. You can turn on Session debugging and examine the protocol trace to see if the server is sending any notifications.

What server are you using?

like image 174
Bill Shannon Avatar answered Oct 19 '22 21:10

Bill Shannon


Finally I got the solution.

For Gmail Account. You need to set "New mail notifications on" ON from your Gmail setting page.

like image 27
Nik88 Avatar answered Oct 19 '22 20:10

Nik88


This page can be helpful to understand the issue: https://bugzilla.mozilla.org/show_bug.cgi?id=544439

like image 33
ganoro Avatar answered Oct 19 '22 21:10

ganoro