使用 imap 和节点获取所有未读电子邮件的列表并记录电子邮件发件人和该电子邮件的主题

Get list of all unread emails and log email sender and subject of that email using imap and node

我正在尝试使用 imap 和节点从电子邮件中获取所有未读电子邮件并记录电子邮件发件人和主题。我正在尝试 运行 以下代码:

var imap = new Imap({

    user: Email,
    password: Password,
    host: 'imap.gmail.com',
    port: 993,
    tls: true
});

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

openInbox(function(err, box) {
    if (err) throw err;
    var f = imap.seq.fetch(box.messages.total + ':*', { bodies: ['HEADER.FIELDS (FROM)','TEXT'] });
    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');
                    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();
    });
});

当我 运行 此代码时,出现以下错误:

Uncaught Error: Not authenticated
    at Connection.openBox

这个错误是什么意思?我确保在我尝试访问的每个 gmail 帐户上启用不太安全的应用程序访问。我将不胜感激任何人的帮助

假设您使用的是 node-imap 模块,您没有正确调用它。

如果您在建立连接之前尝试调用 openBox,就会发生此错误。 (也就是说,您已连接并通过身份验证,并准备好打开邮箱)。

你需要做两件事:

  1. 添加对 imap.connect()
  2. 的顶级调用
  3. 将您当前的大部分 openInbox 调用包含在对 imap.once() 的调用中,如下所示:
imap.once('ready', function() {
  openInbox(function(err, box) {
    ...
  });
});

我建议您从他们的文档中的 the exact example 开始,直到您确信您已经正确设置并正常工作,这样您就可以将代码问题与 gmail 身份验证问题区分开来。

除此之外,正如您所指出的,Gmail 对您使用低安全性应用程序的方式非常善变。使用普通客户端来证明您已启用低安全性应用程序设置、您已处理任何需要执行的解锁验证码等可能是有意义的。参见full list。特别是,在开始工作之前,您可能需要在安全控制台中手动批准其中一项登录。

FWIW,如果您已连接但由于 Gmail 不喜欢登录而未进行身份验证,则出现的错误是不同的,它看起来更像这样:

{ Error: Please log in via your web browser: https://support.google.com/mail/accounts/answer/78754 (Failure)
    at Connection._resTagged (/.../lib/Connection.js:1502:11)
    at Parser.<anonymous> (/.../lib/Connection.js:194:10)