爪哇邮件不能收到所有邮件

2022-04-01 00:00:00 java jakarta-mail

我正在尝试获取我的Gmail的所有邮件,但我只能收到250封。根据https://javaee.github.io/javamail/FAQ#gmailsettings,我已经将邮件数量限制设置为1000封,但它仍然只能提取250封邮件。以下是我的代码

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();
  }
  }

  }

有人知道我怎样才能得到全部吗?


解决方案

我刚刚测试过,如果您不需要使用POP,可以试试IMAP 您的Chech方法将如下所示。我确实测试过了,我可以不受限制地收发邮件。而且,如果你将你的Gmail账户配置为一个特定的号码,你也会收到这个号码。

 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();
          }
          }

相关文章