Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7538161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:02:54+00:00 2026-05-30T07:02:54+00:00

I am doing on task to retrieve gmails. I manage to retrieve with below

  • 0

I am doing on task to retrieve gmails. I manage to retrieve with below codes. However, it retrieved received email from the earliest to latest emails in the gmail inbox. Is there a way to make it retrieved the latest mails? I intend to implement a way to retrieved the latest 20 mails only instead of retrieved all the mails in the inbox. Thanks in advance for the guidance.

public ArrayList<HashMap<String, String>> getMail(int inboxList){
        Folder inbox;
         /*&nbsp; Set the mail properties&nbsp; */
         Properties props = System.getProperties();
         props.setProperty("mail.store.protocol", "imaps");
         try
         {
             /*&nbsp; Create the session and get the store for read the mail. */
             Session session = Session.getDefaultInstance(props, null);
             Store store = session.getStore("imaps");
             store.connect("imap.gmail.com",username, password);

             /*&nbsp; Mention the folder name which you want to read. */
             inbox = store.getFolder("Inbox");
             System.out.println("No of Unread Messages : " + inbox.getUnreadMessageCount());
             /*Open the inbox using store.*/
             inbox.open(Folder.READ_WRITE);
             /*&nbsp; Get the messages which is unread in the Inbox*/
             Message messages[];
             if(recent){
                 messages = inbox.search(new FlagTerm(new Flags(Flag.RECENT), false));
             }else{
                 messages = inbox.search(new FlagTerm(new Flags(Flag.SEEN), false));
             }

             /* Use a suitable FetchProfile&nbsp;&nbsp;&nbsp; */
             FetchProfile fp = new FetchProfile();
             fp.add(FetchProfile.Item.ENVELOPE);
             fp.add(FetchProfile.Item.CONTENT_INFO);
             inbox.fetch(messages, fp);

             try
             {
                 printAllMessages(messages);
                 inbox.close(true);
                 store.close();
             }
             catch (Exception ex)
             {
                 System.out.println("Exception arise at the time of read mail");
                 ex.printStackTrace();
             }
         }
         catch (NoSuchProviderException e)
         {
             //e.printStackTrace();

             System.exit(1);

         }
         catch (MessagingException e)
         {
             //e.printStackTrace();

             System.exit(2);
         }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T07:02:55+00:00Added an answer on May 30, 2026 at 7:02 am

    Instead of using inbox.search() use

     inbox.getMessages(int start,int end);
    

    it will retrieve the range of messages.

    To get the latest 20 mails use:

    int n=inbox.getMessageCount();
    messages= inbox.getMessages(n-20,n);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently doing a task where I'm taking forms from a local government body,
I've been given the task of doing some work customizing an artist's space in
I was given the task of doing a facelift to our current site. I
we're transitioning to doing all our task tracking in TFS 2010 and I'm wondering
While doing some small regex task I came upon this problem. I have a
I'm having difficulty doing this seemingly simple task. I want to load XML files
I'm doing a windows form and would like an audit task to happen every
Doing the below will reproduce my problem: New WPF Project Add ListView Name the
How do I use a progress bar when my script is doing some task
I have a script task in an SSIS package that is supposed to retrieve

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.