简体   繁体   English

在Spring Boot中使用IMAP访问来自Gmail的电子邮件

[英]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. 我正在尝试在JavaMail API的帮助下通过IMAP从Gmail帐户访问电子邮件。

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... 希望这对您有帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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