简体   繁体   English

使用JavaMail API如何从GMail提取前20个邮件,然后提取接下来的20个邮件?

[英]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. 我是JavaMail API的新手,目前正从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 = "yourmail@gmail.com";// 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 ** 所以现在我在输出中得到整个邮件,因为**我正在使用条件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. 我想要的是我只想阅读20条消息并将其显示在jtable中,然后,如果我想要更多,则仅阅读下一条20条消息,依此类推。 How can I do that?? 我怎样才能做到这一点??

I am thinking about creating a loop with i < 20 but what about the next 20? 我正在考虑创建一个i <20的循环,但是下一个20呢? How to read the next 20 mail not again reading the mail from start??? 如何阅读接下来的20封邮件,而不是从头开始阅读邮件?

There is a method on javax.mail.Folder that allows you to retrieve messages from a start/end. javax.mail.Folder上有一个方法,允许您从开始/结尾检索消息。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM