【问题标题】:Use node-imap to retrieve emails使用 node-imap 检索电子邮件
【发布时间】:2019-05-26 19:22:46
【问题描述】:

这是一个模块 retrieve_email.js,它连接到我的 gmail 帐户并在某个日期后下载 UNSEEN 电子邮件。该代码几乎是从[imap 模块]1 的示例中复制而来的。

const Imap = require('imap');
const inspect = require('util').inspect;
const simpleParser = require('mailparser').simpleParser;

const imap = new Imap({
  user: 'mygmail@gmail.com',
  password: 'mypassword',
  host: 'imap.gmail.com',
  port: 993,
  tls: true
});

function openInbox(callback) {
    imap.openBox('INBOX', true, callback);
};

async function parse_email(body) {
  let parsed = simpleParser(body);
  ...............
};

module.exports = function() {
  imap.once('ready', function() {
    openInbox(function(err, box) {
      if (error) throw err;

      imap.search(['UNSEEN', ['SINCE', 'May 20, 2018']], function(err, results){
        if (err) throw err;
        var f = imap.fetch(results, {bodies: ''});
        f.on('message', function(msg, seqno) {
          console.log('Message #%d', seqno);
          var prefix = '(#' + seqno + ') ';
          msg.on('body', function(stream, info) {
            if (info.which === 'TEXT')
              console.log(prefix + 'Body [%s] found, %d total bytes', inspect(info.which), info.size);
              var buffer = '', count = 0;
              stream.on('data', function(chunk) {
                count += chunk.length;
                buffer += chunk.toString('utf8');
                parse_email(buffer);
                if (info.which === 'TEXT')
                  console.log(prefix + 'Body [%s] (%d/%d)', inspect(info.which), count, info.size);
              });
              stream.once('end', function() {
                if (info.which !== 'TEXT')
                  console.log(prefix + 'Parsed header: %s', inspect(Imap.parseHeader(buffer)));
                else
                  console.log(prefix + 'Body [%s] Finished', inspect(info.which));
              });
          });
          msg.once('attributes', function(attrs) {
            console.log(prefix + 'Attributes: %s', inspect(attrs, false, 8));
          });
          msg.once('end', function() {
            console.log(prefix + 'Finished');
          });
        });

        f.once('error', function(err) {
          console.log('Fetch error: ' + err);
        });

        f.once('end', function() {
          console.log('Done fetching all messages');
          imap.end();
        });

      });
    });
  });

  imap.once('error', function(err) {
    console.log(err);
  });

  imap.once('end', function() {
    console.log('Connection ended');
  });

  imap.connect();
};

index.js中调用模块时,在debug中可以看到代码是从上往下扫描的,最后扫描到的代码行是imap.connect(),然后又回到index.js中的下一行,没有连接到 gmail 帐户,也没有检索电子邮件的操作。上面的代码有什么问题?

更新:socket.connect() 之后的状态在调试中:

【问题讨论】:

  • 我不知道那个库,但我只是做了一个函数来使用 Nodemailer 通过 Gmail 发送电子邮件,并且我正在使用 Oauth 进行身份验证,另一个选项是允许不安全的连接。我没有看到您在任何地方使用 Oauth,所以您是否授权了与 Gmail 的不安全应用连接?
  • Simon Cadieuximap定义中有登录信息。据我了解,Nodemailer 不能用于检索 imap 电子邮件。
  • 代码看起来没什么问题,所以需要调试一下。它是否能够连接,或者您是否有一个数据包过滤器使连接尝试挂起几分钟?等等。寻找那种不幸。 tcpdump 或 wireshark 可能会很方便。
  • 你能添加来自index.js的代码吗?
  • arnt, Styx,我将所有imap 代码移到index.js 中,并在调试中对其进行了多次调试。我可以看到imap 的值被传递到 imap.connect() 中。但是openInbox 从未被调用和执行。没有错误抛出。我不太明白imap 模块是如何工作的。但在我看来,电子邮件检索(openInbox)从未发生过

标签: node.js imap node-imap


【解决方案1】:

看看这个,这是来自 Google 的 Gmail API 参考。在该页面上有一个如何使用 Node.js 连接到它的示例。

https://developers.google.com/gmail/api/quickstart/nodejs

这是来自同一文档的示例,向您展示如何使用 q 参数搜索和检索消息列表:

https://developers.google.com/gmail/api/v1/reference/users/messages/list

附:在我的评论中,我只是问您是否确定您已完成通过代码访问您的 Gmail 帐户所需的所有其他配置,这意味着创建应用程序、授权 OAuth 或在您的情况下似乎授权不太安全的应用程序访问,只需查看您可能会发现缺少某些内容的链接。

你真的需要使用 IMAP 包吗???

【讨论】:

  • 是的,我需要使用与特定网络邮件提供商无关的解决方案。这就是我选择节点 imap 模块的原因。当有一个新的网络邮件提供商时,我需要做的是在imap 定义中提供新信息,我可以从另一个网络邮件提供商检索电子邮件。还是谢谢你!
【解决方案2】:

发现的问题是 Avast mail shield 作为中间人拦截 IMAP 流量并导致 HTTPS 失败。此外,IDE 调试器会在某处停止以保持连接处于活动状态但尚未准备好。这是detail of the solution

【讨论】:

    猜你喜欢
    • 2011-04-06
    • 2016-05-11
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    • 2011-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多