Listening for Java Messages for POP3 Messages

I am trying to listen to new messages using the POP3 protocol. I know that Pop3 does not allow new messages to appear in the inbox while the folder is open. Below is the code that I executed:

import javax.mail.event.MessageCountAdapter; import javax.mail.event.MessageCountEvent; public class EmailListener extends MessageCountAdapter { public EmailListener() { } public void messagesAdded(MessageCountEvent e) { System.out.println("I"); } public void messagesRemoved(MessageCountEvent e) { System.out.println("J"); } } public class POPReceiver { public POPReceiver() { } public void listen() throws Exception { Properties properties = new Properties(); Session session = null; POP3Store pop3Store = null; String host = "NB-EX101.example.com"; String user = "user2"; properties.put(mail.pop3.host, host); session = Session.getDefaultInstance(properties); pop3Store = (POP3Store) session.getStore("pop3"); pop3Store.connect(user, "password"); Folder folder = pop3Store.getFolder("INBOX"); folder.addMessageCountListener(new EmailListener()); sendEmail(); } public void sendEmail() { // not added code, but the email sends } } public static void main(String[] args) throws Exception { POPReceiver i = new POPReceiver(); i.listen(); } 

I am using Microsoft Exchange Server. Any ideas why he is not listening?

I looked at http://www.coderanch.com/t/597347/java/java/Email-Listener but still not listening.

+4
source share
1 answer

From the Javamail FAQ ( http://www.oracle.com/technetwork/java/javamail/faq/index.html ):


Q : I configured MessageCountListener (as shown in the monitor program), but I never notify of new mail in my POP3 INBOX.

A. The POP3 protocol prevents the client from seeing new messages delivered to INBOX while INBOX is open. The application should close INBOX and reopen it to see new messages. You will never be notified of new mail using the MessageCountListener interface with POP3. See the com.sun.mail.pop3 package documentation for more details.


So MessageCountListener will not work for POP3. You will need to complete a survey to receive information about new messages for POP3.

However, you can try using IMAP.

But even with IMAP, you have to use it differently. See the idle() method in the IMAPStore class (for example, it is called in a loop in a separate thread, etc. - see https://javamail.java.net/nonav/docs/api/com/sun/mail/imap/ IMAPStore.html # idle () ).

+6
source

All Articles