如何从node-imap或node-inbox模块中检索已发送的框项目

时间:2017-11-30 19:34:01

标签: node.js node-imap

这是我的代码,但它不适用于已发送的邮件文件夹。我尝试过使用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);
        });
      });

    });
  });

1 个答案:

答案 0 :(得分:0)

您可以使用mail-listner2 lib相同,下面是用于阅读收件箱邮件的代码段,同样适用于发送框,用SENT替换INBOX。让我知道进展情况。

 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;


}