1

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));
afzalex
  • 8,254
  • 2
  • 29
  • 53
Anand
  • 657
  • 3
  • 12
  • 37

2 Answers2

2

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

Bill Shannon
  • 27,854
  • 5
  • 34
  • 37
  • Thanks for ur suggestion. I changed it to IMAP, but getting an error - Could not connect to the message store javax.mail.AuthenticationFailedException: failed to connect. I used the following code Store store = session.getStore("imap"); store.connect(userName, password); – Anand Oct 15 '14 at 03:37
  • You need to enable IMAP on your gmail account settings sheet. – hd1 Oct 15 '14 at 03:45
  • See also this [JavaMail FAQ entry](http://www.oracle.com/technetwork/java/javamail/faq/index.html#gmail). – Bill Shannon Oct 15 '14 at 21:16
  • @BillShannon: The testing piece with gmail account works fine. When I try to connect to the client mailbox, its giving an error --- java.net.ConnectException: Connection refused: connect at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:665). FYI, the mail id is registered with the mail server and so it does not require the passwords to connect to. I get an error @ this place store.connect("HOST", userName, "pwd"); – Anand Oct 16 '14 at 03:02
  • Please spend more time with the JavaMail FAQ. It includes [tips for debugging connection problems](http://www.oracle.com/technetwork/java/javamail/faq/index.html#condebug). I don't know what you mean by "the mail id is registered with the mail server", but it's very unlikely that Gmail will allow you to do anything without supplying your password. – Bill Shannon Oct 16 '14 at 06:55
0

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.

Community
  • 1
  • 1
logicalicy
  • 609
  • 1
  • 7
  • 15
  • @logiicalicy: I went through that link already and so was able to figure out certain things. I tried with SEEN option too. As I said in my Original Post, the message does not have any flags assigned to it and so it is not considering SEEN or RECENT. The suggestions to change the settings does not say wot to change. I tried multiple options in "Forwarding and POP/IMAP", but could not. – Anand Oct 15 '14 at 03:35