Search code examples
javaemailjakarta-mail

Using JavaMail API how to fetch first 20 mail and then the next 20 mails from GMail?


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

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 < 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 = "[email protected]";// change accordingly
  String password = "*****";// change accordingly

  check(host, mailStoreType, username, password);

}

}

So now I am getting entire mail in the output since ** I am using a loop with condition i < message.length **

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??

I am thinking about creating a loop with i < 20 but what about the next 20? How to read the next 20 mail not again reading the mail from start???


Solution

  • There is a method on javax.mail.Folder that allows you to retrieve messages from a start/end. You'd just need to keep track of the last position you retrieved from.

    public Message[] getMessages(int start,
                             int end)
                      throws MessagingException
        Get the Message objects for message numbers ranging from start through end, both start and end inclusive. Note that message numbers start at 1, not 0.
        Message objects are light-weight references to the actual message that get filled up on demand. Hence Folder implementations are expected to provide light-weight Message objects.
    
        This implementation uses getMessage(index) to obtain the required Message objects. Note that the returned array must contain (end-start+1) Message objects.
    
        Parameters:
            start - the number of the first message
            end - the number of the last message
        Returns:
            the Message objects
        Throws:
            FolderNotFoundException - if this folder does not exist.
            IllegalStateException - if this folder is not opened.
            java.lang.IndexOutOfBoundsException - if the start or end message numbers are out of range.
            MessagingException