We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to get latest unread mails from inbox using Aspose.Email for java

Hi Team,
we’re trying to read the latest unread messages from gmail using IMAP using ASPOSE.Email for
java . But we’re unable to fetch latest unread messages . Please provide the java code snippet for the same.

Thank you
Phanikumar P

1 Like


For fetching email messages, you may use the following code example:

ImapClient client = new ImapClient();

ImapMessageInfoCollection coll = client.listMessages();

We hope that this answered your query. Please feel free to write us if you require additional information or you have any query in future.

Using the above code can we read UnRead emails in the Gmail inbox or all inbox emails?
Our Requirement is read only Unread emails in GMail Inbox bucket


This can be achieved by using these possible ways:

  1. Using ImapQueryBuilder. Please find the code sample, you may also need to flag all read messages.

     ImapQueryBuilder builder = new ImapQueryBuilder();
     //Check if not Flagged for IsRead
     //Email that arrived today
     builder.getInternalDate().on(new Date());
     //Build the query
     MailQuery query = builder.getQuery();
     //Get list of messages
     ImapMessageInfoCollection messages = client.listMessages(query); 
  2. It can be achieved by getting the value of IsRead property for every message, like as:

     ImapMessageInfoCollection messageInfos = client.listMessages();
     for (ImapMessageInfo inf: messageInfos)
         if (!inf.isRead())
             System.out.println("This is Unread!");