Anand
Anand

Reputation: 707

How to read the UNREAD email using java mail api

I am trying to read the UNREAD mails from gmail account. I tried to filter the messages based on the Flags, but it did not work. I printed the Flags sent on each message, but nothing is set on it, because of which I could not filter the messages. I used the keyword Flags.Flag.SEEN to filter the messages. After googling, I found out that it is something with the email client. For ex, we need to the change the configuration in gmail or any exchange mail server. Can you please tell me on what to change the configuration to read the UNREAD mails?

Also, end of day, I am going to implement the code into one of the smtp exchange servers. Please let me know if there needs a special configuration. So that I can inform the respective team and then implement my change.

       // connects to the message store
        Store store = session.getStore("pop3");
        store.connect(userName, password);

        // opens the inbox folder
        Folder folderInbox = store.getFolder("INBOX");
        System.out.println("unread count - " + folderInbox.getUnreadMessageCount());
        folderInbox.open(Folder.READ_WRITE);
        // folderInbox.search(new FlagTerm(new Flags(Flags.Flag.RECENT),
        // false));

        // fetches new messages from server
        Message[] arrayMessages = folderInbox.search(new FlagTerm(new Flags(Flags.Flag.RECENT), false));

Upvotes: 1

Views: 4440

Answers (2)

Bill Shannon
Bill Shannon

Reputation: 29961

The POP3 protocol doesn't support any flags. Use IMAP.

Upvotes: 2

logicalicy
logicalicy

Reputation: 907

See Retrieving all unread emails using javamail with POP3 protocol.

One comment suggests the use of Flags.Flag.SEEN for Gmail. There's another suggestion about changing settings in your test Gmail account.

Upvotes: 0

Related Questions