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
+91-9985079507

1 Like

@nvphani

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

ImapClient client = new ImapClient();
client.setHost("imap.gmail.com");
client.setPort(993);
client.setUsername("username");
client.setPassword("password");
client.setSecurityOptions(SecurityOptions.Auto);

client.selectFolder(ImapFolderInfo.IN_BOX);
ImapMessageInfoCollection coll = client.listMessages();
		
System.out.println(coll.size()); 

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.

@MuzammilKhan
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

@VKChikkadamalla

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
     builder.hasNoFlags(ImapMessageFlags.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!");
     }