【问题标题】:How can I get only unread emails using imapflow?如何使用 imapflow 仅获取未读电子邮件?
【发布时间】:2021-06-03 22:45:27
【问题描述】:

早上好,我在工作中使用这个库,但遇到了问题。

当我连接到 imapflow 邮件服务器时,它会返回所有邮件,但我无法区分哪些邮件已读,哪些未读。

我一直在阅读文档 (https://imapflow.com/),但找不到任何对我有帮助的东西。我也尝试过使用过滤器,但没有任何效果。

你能帮帮我吗?

我将代码 sn-p 留在我试图获取未读电子邮件的位置。

非常感谢您的帮助。

    const main = async (client) => {

let envelope = [];

await client.connect();

// At this point I have set the variable unseen to true but it doesn't work :(
let lock = await client.getMailboxLock('INBOX', {unseen: true});

try {
    let message = await client.fetchOne('*', { source: true });
    //console.log(message.source.toString());

    for await (let message of client.fetch('1:*', { envelope: true })) {
        
        envelope.push(message.envelope)
    }
} finally {
    lock.release();
}

await client.logout();

let reverse_envelope = envelope.reverse();

return reverse_envelope
};

【问题讨论】:

  • 我不知道这个库,但是关于消息的元数据包含在“FLAGS”获取项中。 \Seen 告诉您它是否已被阅读,\Flagged 如果用户将其标记为重要,等等。

标签: node.js imap


【解决方案1】:

使用imapflow,解决方法很简单:

您可以使用client.fetch() 方法和SearchObject 作为第一个参数。作为第二个参数,你可以使用FetchQueryObject:

await client.fetch({ seen: false }, { envelope: true });

您作为FetchQueryObject 传递的内容可能会有所不同,您也可以将不同的选项传递给SearchObject,更多信息可以在docs 中找到:)

我正在使用此代码:

let emails; 
for await (let msg of client.fetch(
      { seen: false },
      {
        flage: true,
        envelope: true,
        source: false,
        bodyParts: true,
        bodyStructure: true,
        uid: true, 
      }
    )) {
      emails.push(msg);
    }
// use emails here

但是:请确保您仅在当前锁定邮箱时使用此代码,否则您将始终得到一个空数组作为结果。

【讨论】:

    猜你喜欢
    • 2017-01-21
    • 1970-01-01
    • 1970-01-01
    • 2019-07-09
    • 2016-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-28
    相关资源
    最近更新 更多