gpt4 book ai didi

java - 使用 JavaMail API 如何从 GMail 中获取前 20 封邮件和接下来的 20 封邮件?

转载 作者:行者123 更新时间:2023-11-29 07:46:27 24 4
gpt4 key购买 nike

我是 JavaMail API 的新手,目前正在从 Tutorialspoint 学习。现在我可以使用以下代码从我的邮件中获取所有电子邮件

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

}

}

所以现在我在输出中得到了整封邮件,因为 ** 我正在使用条件为 i < message.length ** 的循环 **

我想要的是我只想读取 20 条消息并将其显示在 jtable 中,然后如果我想要更多,则只读取下 20 条消息,依此类推。我该怎么做??

我正在考虑创建一个 i < 20 的循环,但接下来的 20 个呢?如何阅读接下来的 20 封邮件而不是从头开始阅读邮件???

最佳答案

javax.mail.Folder 上有一个方法允许您从开始/结束检索消息。您只需要跟踪您检索的最后位置。

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

关于java - 使用 JavaMail API 如何从 GMail 中获取前 20 封邮件和接下来的 20 封邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25215272/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com