1

I am trying to access emails from Gmail accounts through IMAP with the help of the JavaMail API.

I am able to access the Inbox folder of both email accounts. But i want to view only not read message, and the number of it, it is possible ? Thank you in advance.

Here is the code:

  // retrieve the messages from the folder in an array and print it
  Message[] messages = emailFolder.getMessages();
  System.out.println("messages.length---" + messages.length);

  for (int i = 0, n = messages.length; i < n; i++) {
     Message message = messages[i];
     System.out.println("---------------------------------");
     System.out.println("Email Number " + (i + 1));
     System.out.println("Subject: " + message.getSubject());
     System.out.println("From: " + message.getFrom()[0]);
     System.out.println("Text: " + message.getContent().toString());

  }
LaymoO
  • 125
  • 1
  • 2
  • 9

1 Answers1

1

The following line will give unread messages count

System.out.println("unread count - " + folder.getUnreadMessageCount());

and the following line will give you all unread messages

Message[] unreadMessages = folder.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));

hope this helps you...

Pavan Kumar K
  • 1,360
  • 9
  • 11