gpt4 book ai didi

java - 使用javamail批量读取邮件

转载 作者:行者123 更新时间:2023-11-30 04:25:52 25 4
gpt4 key购买 nike

我有以下代码,我想批量检索(例如:获取前 50 条消息,处理它,然后获取下 50 条)。目前它获取所有消息并将其存储在一个数组中。Javamail 支持吗?如果不是如何批量检索?

感谢您的回答。

Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");

Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect(host, userName, password);

Folder inbox = null;
inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_WRITE);
Message[] messages = inbox.search(new FlagTerm(
new Flags(Flag.SEEN), false));
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
fp.add(FetchProfile.Item.CONTENT_INFO);
inbox.fetch(messages, fp);
for (int i = 0; i < messages.length; i++)
{
//Process a message
}

更新:

我尝试按如下方式实现批处理,但它没有按预期工作。

问题是:

  • 假设收件箱中有 24 封电子邮件,则 totalUnread 显示正确,但 Message[] messages = inbox.search(new FlagTerm(new Flags(Flag.SEEN),
    false), inbox.getMessages(batchStart, batchEnd));
    仅返回 5 条记录,而不是 10 条记录,因为 BATCH_SIZE 为 10。

  • 另一个问题是,即使调用 getContent(),处理的电子邮件也不会标记为已读。

    private static final int BATCH_SIZE = 10;
    Properties props = System.getProperties();
    props.setProperty("mail.store.protocol", "imaps");
    AuthenticationService authenticationService = null;
    Session session = Session.getDefaultInstance(props, null);
    Store store = session.getStore("imaps");
    store.connect(host, userName, password);
    Folder inbox = null;
    inbox = store.getFolder("Inbox");
    int totalUnread = inbox.getUnreadMessageCount();
    if (totalUnread != 0)
    {
    inbox.open(Folder.READ_WRITE);
    int batchStart = 1;
    int batchEnd = (totalUnread > BATCH_SIZE ? BATCH_SIZE
    : totalUnread);
    int batchCount = 0;
    while (true)
    {
    processABatch(inbox, batchStart, batchEnd, batchCount);
    batchStart = batchEnd + 1;
    if (batchStart > totalUnread)
    {
    break;
    }
    batchEnd = ((batchEnd + BATCH_SIZE) < totalUnread ? (batchEnd + BATCH_SIZE)
    : totalUnread);
    batchCount++;
    }

    }
    inbox.close(true);
    store.close();
    }

    private void processABatch(Folder inbox, int batchStart, int batchEnd, int batchCount)
    throws MessagingException
    {
    Message[] messages = inbox.search(new FlagTerm(new Flags(Flag.SEEN),
    false), inbox.getMessages(batchStart, batchEnd));
    FetchProfile fp = new FetchProfile();
    fp.add(FetchProfile.Item.ENVELOPE);
    fp.add(FetchProfile.Item.CONTENT_INFO);
    inbox.fetch(messages, fp);
    for (int i = 0; i < messages.length; i++)
    {
    processMessage(messages[i], inbox);
    }
    }

最佳答案

您可能不想执行 inbox.search(new FlagTerm(...)),而是执行 inbox.search(new FlagTerm(...), getMessages(start,结束))。它使用 getMessages(int, int)方法,允许您检索当前文件夹中所有消息的子集。

事实上,getMessages(start, end) 适用于整个文件夹。根据该方法的 Javadoc,Message 对象应提供轻量级,因为它们只是对实际消息的引用。

因此,也许您可​​以编写一个方法,通过连续获取消息并将其放入 List 或类似的内容中,返回前 50 条未读消息,直到您其中有 50 封邮件,或者您已到达文件夹的末尾。该消息的结果将是“批处理”。

之后,您可以对消息进行常规处理。

关于java - 使用javamail批量读取邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15877308/

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