node.js - Unable to retrieve email using message id in imap, node js - Stack Overflow

The Problem:When I try to search for an email using Message-ID, the search returns zero results.Even

The Problem: When I try to search for an email using Message-ID, the search returns zero results. Even though I explicitly set a Message-ID while sending the email, it only appears inside text/rfc822-headers or message/rfc822 when fetching emails with struct: true. The same query works for other headers (like Subject) but not for Message-ID.

Current IMAP Logic:

// Open inbox in read/write mode
imap.openBox("INBOX", false, (error) => {
  if (error) throw error;
  console.log("Inbox opened");

  // Listen for new emails
  imap.on("mail", function () {
    console.log("New email received");

    // Search for email by Message-ID
    imap.search([["HEADER", "MESSAGE-ID", messageId]], function (error, uids) {
      if (error) throw error;
      console.log("Search results: ", uids);

      if (uids.length > 0) {
        const f = imap.fetch(uids, { bodies: "", struct: true });

        f.on("message", function (message) {
          message.on("body", (stream) => {
            const buffer: Buffer[] = [];
            stream.on("data", (chunk) => buffer.push(chunk));
            stream.on("end", async () => {
              const parsed = await simpleParser(Buffer.concat(buffer));
              console.log(parsed);
            });

            message.on("attributes", (attrs) => {
              imap.setFlags(attrs.uid, ["\\Seen"], (error) =>
                console.log(error),
              );
            });
          });
        });
      }
    });
  });
});

Question: Why does imap.search([["HEADER", "MESSAGE-ID", messageId]]) return no results? Is Message-ID not indexed by some IMAP servers like GoDaddy? Would searching inside rfc822-headers be a better approach? Would appreciate any insights or workarounds!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744604925a4583407.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信