mail-listener2不起作用

时间:2014-08-16 23:20:26

标签: node.js

这是我的邮件监听器文件的代码。

我在我的主文件中调用了startListening函数,我可以阅读" Imap connected"在控制台中,但是,即使有些电子邮件到达,也没有任何反应。

任何想法?

var MailListener = require("mail-listener2");

var mailListener = new MailListener({
  username: "myEmail@gmail.com",
  password: "myPassword",
  host: "imap.gmail.com",
  port: 993, // imap port
  tls: true,
  tlsOptions: { rejectUnauthorized: false },
  mailbox: "INBOX", // mailbox to monitor
  searchFilter: ["UNSEEN", "FLAGGED"], // 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
});

module.exports.startListening = function(){
    mailListener.start(); // start listening
}

// stop listening
//mailListener.stop();

mailListener.on("server:connected", function(){
  console.log("imapConnected");
});

mailListener.on("server:disconnected", function(){
  console.log("imapDisconnected");
});

mailListener.on("error", function(err){
  console.log(err);
});

mailListener.on("mail", function(mail, seqno, attributes){
  // do something with mail object including attachments
  console.log("emailParsed", mail);
  // mail processing code goes here
});

mailListener.on("attachment", function(attachment){
  console.log(attachment.path);
});

2 个答案:

答案 0 :(得分:9)

我有同样的问题。这个例子不起作用。我改为使用mail-notifier

var notifier = require('mail-notifier');

var imap = {
      user: "_example@example.com_",
      password: "password",
      host: "imap.gmail.com",
      port: 993,
      tls: true,
      tlsOptions: { rejectUnauthorized: false }
};

notifier(imap).on('mail',function(mail){
         console.log("GOT MAIL");
}).start();

像魅力一样工作

答案 1 :(得分:5)

要使代码段工作,请删除:

searchFilter: "FLAGGED"

目前,您只是搜索看不见的,已标记的电子邮件,并且可能未将传入的电子邮件标记为重要。我有上面的确切实现,没有FLAGGED搜索过滤器,这非常有效。