title: Using JavaMail API how to fetch first 20 mail and then the next 20 mails from GMail?
body: <p>I am new to JavaMail API and currently learning from Tutorialspoint. Right now I can get all my emails from my mail using the following code</p>
  
  <pre><code>import java.util.Properties;
  
  import javax.mail.Folder;
  import javax.mail.Message;
  import javax.mail.MessagingException;
  import javax.mail.NoSuchProviderException;
  import javax.mail.Session;
  import javax.mail.Store;
  
  public class CheckingMails {
  
  public static void check(String host, String storeType, String user,
    String password) 
  {
    try {
  
    Properties properties = new Properties();
  
    properties.put("mail.pop3.host", host);
    properties.put("mail.pop3.port", "995");
    properties.put("mail.pop3.starttls.enable", "true");
    Session emailSession = Session.getDefaultInstance(properties);
  
    Store store = emailSession.getStore("pop3s");
  
    store.connect(host, user, password);
  
    Folder emailFolder = store.getFolder("INBOX");
    emailFolder.open(Folder.READ_ONLY);
  
    Message[] messages = emailFolder.getMessages();
    System.out.println("messages.length---" + messages.length);
  
    for (int i = 0, n = messages.length; i &lt; 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());
  
    }
  
    emailFolder.close(false);
    store.close();
  
    } catch (NoSuchProviderException e) {
       e.printStackTrace();
    } catch (MessagingException e) {
       e.printStackTrace();
    } catch (Exception e) {
       e.printStackTrace();
    }
  }
  
  public static void main(String[] args) {
  
    String host = "pop.gmail.com";// change accordingly
    String mailStoreType = "pop3";
    String username = "yourmail@gmail.com";// change accordingly
    String password = "*****";// change accordingly
  
    check(host, mailStoreType, username, password);
  
  }
  
  }
  </code></pre>
  
  <p>So now I am getting entire mail in the output since ** I am using a loop with condition i &lt; message.length **</p>
  
  <p>What I want is I wanna read just 20 messages and display it in a jtable and then if I want more then only read the next 20 message and so on. How can I do that??</p>
  
  <p>I am thinking about creating a loop with i &lt; 20 but what about the next 20? How to read the next 20 mail not again reading the mail from start???</p>