Search code examples
javajakarta-mail

Javamail doesn't get all mail


I'm trying to get all mails of my gmail but I can only get 250. According to https://javaee.github.io/javamail/FAQ#gmailsettings, I had already set the number of mail limit to 1000 but it still only fetch 250 mails. Here's my 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 Getreply {

  public static void check() {

  String host = "pop.gmail.com";
  String mailStoreType = "pop3";
  String username = "";
  String password = "";
  try {

  //create properties field
  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);

  //create the POP3 store object and connect with the pop server
  Store store = emailSession.getStore("pop3s");

  store.connect(host, username, password);

  //create the folder object and open it
  Folder emailFolder = store.getFolder("INBOX");
  emailFolder.open(Folder.READ_ONLY);

  // retrieve the messages from the folder in an array and print it
  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];
     Object body = message.getContent(); 
     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: " + body );

  }

  //close the store and folder objects
  emailFolder.close(false);
  store.close();

  } catch (NoSuchProviderException e) {
     e.printStackTrace();
  } catch (MessagingException e) {
     e.printStackTrace();
  } catch (Exception e) {
     e.printStackTrace();
  }
  }

  }

Does anyone know how can I get all?


Solution

  • I have just test it if you have no need to use pop you should try imap your chech method will be like this below. Idid test it i can my mails without restriction. But also if you configure your gmail account for a specific number you will receive that number.

     public static void check() {
    
              String host = "imap.gmail.com";
              String username = "user";
              String password = "passwd";
              try {
    
              //create properties field
              Properties properties = new Properties();
    
    
              properties.setProperty("mail.imap.ssl.enable", "true");
              Session emailSession = Session.getDefaultInstance(properties);
    
              //create the POP3 store object and connect with the pop server
              Store store = emailSession.getStore("imap");
    
              store.connect(host, username, password);
    
              //create the folder object and open it
              Folder emailFolder = store.getFolder("INBOX");
              emailFolder.open(Folder.READ_ONLY);
    
              // retrieve the messages from the folder in an array and print it
              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];
                 Object body = message.getContent(); 
                 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: " + body );
    
              }
    
              //close the store and folder objects
              emailFolder.close(false);
              store.close();
    
              } catch (NoSuchProviderException e) {
                 e.printStackTrace();
              } catch (MessagingException e) {
                 e.printStackTrace();
              } catch (Exception e) {
                 e.printStackTrace();
              }
              }