【问题标题】:How to retrieve sent box items from node-imap or node-inbox module如何从 node-imap 或 node-inbox 模块中检索已发送的盒子项目
【发布时间】:2025-12-20 19:00:17
【问题描述】:

这是我的代码,但它不适用于已发送的消息文件夹。我曾尝试使用 node-imap 和 node-inbox 以及 node-maillistner2 模块,但它们都无法检索已发送的消息框。我正在使用 ymail 帐户和 imap 服务器。

  var inbox = require("inbox");

  console.log(mailLogin);
  var client = inbox.createConnection(false, mailLogin.imapserver, {
    secureConnection: mailLogin.isTlsEnabled,
    auth: {
      user: mailLogin.email,
      pass: mailLogin.password
    }
  });

  client.connect();
  client.listMailboxes({all:true}, function (error, info) {
    console.log(info)
  })

  client.on("connect", function () {
    client.openMailbox("INBOX/SENT", function (error, info) {
      if (error) throw error;

      client.listMessages(-10, function (err, messages) {
        messages.forEach(function (message) {
          console.log(message.UID + ": " + message.title);
        });
      });

    });
  });

【问题讨论】:

  • 您是遇到错误,还是listMessages 只是返回 0 条消息?

标签: node.js node-imap


【解决方案1】:

你也可以使用 mail-listner2 lib,下面是读取收件箱消息的代码 sn-p,同样适用于发送框,将 INBOX 替换为 SENT。让我知道进度。

 emailbody(userid, password) {



        var deferred = protractor.promise.defer();

        var MailListener = require("mail-listener2");
        var mailListener = new MailListener({
            username: userid,
            password: password,
            host: "imap.gmail.com",
            //host: "imap.gmx.com",
            port: 993, // imap port
            tls: true,
            connTimeout: 25000, // Default by node-imap
            authTimeout: 10000, // Default by node-imap,
            debug: console.log, // Or your custom function with only one incoming argument. Default: null
            tlsOptions: { rejectUnauthorized: false },
            mailbox: "INBOX", // mailbox to monitor
            searchFilter: ["UNSEEN"], // the search filter being used after an IDLE notification has been retrieved
            markSeen: true, // all fetched email willbe marked as seen and not fetched next time
            fetchUnreadOnStart: true, // use it only if you want to get all unread email on lib start. Default is `false`,
            mailParserOptions: { streamAttachments: true }, // options to be passed to mailParser lib.
            attachments: true, // download attachments as they are encountered to the project directory
            attachmentOptions: { directory: "attachments/" } // specify a download directory for attachments

        });

        global.mailListener = mailListener;

        mailListener.start();


        mailListener.on("mail", function (mail, seqno, attributes) {
            console.log('email received!');
            global.mailListener.stop();
            deferred.fulfill(mail);
        });

        mailListener.on("error", function (err) {
            console.log('Email reading error');
            global.mailListener.stop();
            deferred.reject(err);
        });
    return deferred.promise;


}

【讨论】: