简体   繁体   中英

Accessing emails from gmail using IMAP in Spring Boot

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());

  }

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM