NodeJS 我无法使用imap连接到Gmail

8cdiaqws  于 2023-01-04  发布在  Node.js
关注(0)|答案(4)|浏览(265)

我尝试使用imap查看我的gmail收件箱中收到的电子邮件的内容,但当我运行代码时,我得到以下错误。

    • 错误**
Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed
    at doWrite (_stream_writable.js:427:19)
    at clearBuffer (_stream_writable.js:551:7)
    at Socket.Writable.uncork (_stream_writable.js:325:7)
    at JSStreamSocket.doWrite (internal/js_stream_socket.js:162:17)
    at JSStream.onwrite (internal/js_stream_socket.js:20:57)
    at Socket.ondata (internal/js_stream_socket.js:64:22)
    at Socket.emit (events.js:210:5)
    at addChunk (_stream_readable.js:309:12)
    at readableAddChunk (_stream_readable.js:290:11)
    at Socket.Readable.push (_stream_readable.js:224:10) {
  code: 'ERR_STREAM_DESTROYED',
  source: 'socket'
}
events.js:187
      throw er; // Unhandled 'error' event
      ^

Error: self signed certificate
    at TLSSocket.onConnectSecure (_tls_wrap.js:1321:34)
    at TLSSocket.emit (events.js:210:5)
    at TLSSocket._finishInit (_tls_wrap.js:794:8)
    at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:608:12)
    at Socket.ondata (internal/js_stream_socket.js:64:22)
    at Socket.emit (events.js:210:5)
    at addChunk (_stream_readable.js:309:12)
    at readableAddChunk (_stream_readable.js:290:11)
    at Socket.Readable.push (_stream_readable.js:224:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:182:23)
Emitted 'error' event on Connection instance at:
    at TLSSocket._onError (C:\Users\debdutgoswami\node_modules\imap\lib\Connection.js:151:10)
    at TLSSocket.emit (events.js:210:5)
    at emitErrorNT (internal/streams/destroy.js:92:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  code: 'DEPTH_ZERO_SELF_SIGNED_CERT',
  source: 'socket'
}
    • 代码**
var Imap = require('imap'),
    inspect = require('util').inspect;

var imap = new Imap({
  user: 'mygmailname@gmail.com',
  password: 'mygmailpassword',
  host: 'imap.gmail.com',
  port: 993,
  tls: true
});

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

imap.once('ready', function() {
  openInbox(function(err, box) {
    if (err) throw err;
    var f = imap.seq.fetch('1:3', {
      bodies: 'HEADER.FIELDS (FROM TO SUBJECT DATE)',
      struct: true
    });
    f.on('message', function(msg, seqno) {
      console.log('Message #%d', seqno);
      var prefix = '(#' + seqno + ') ';
      msg.on('body', function(stream, info) {
        var buffer = '';
        stream.on('data', function(chunk) {
          buffer += chunk.toString('utf8');
        });
        stream.once('end', function() {
          console.log(prefix + 'Parsed header: %s', inspect(Imap.parseHeader(buffer)));
        });
      });
      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();
nfzehxib

nfzehxib1#

我有同样的问题相当长的一段时间...这里真实的的问题是,有一个投诉的自我签名证书,我得到了一条出路。
tls: true(自签名证书)下面,在var imap= new imap({ //inside here })内添加一行:
tlsOptions: { rejectUnauthorized: false }
现在可以开始了,其余的代码都是正确的

acruukt9

acruukt92#

您可能还需要在帐户上允许“不太安全的应用程序”,请参阅https://myaccount.google.com/lesssecureapps

esbemjvw

esbemjvw3#

我也有一个问题,我刚刚发现谷歌自2022年6月以来禁用了“不太安全的应用程序”。简单的用户/密码登录GMAIL已被永久禁用。

jk9hmnmh

jk9hmnmh4#

我遇到了同样的问题,并通过以下方式解决:
Google设置:

  • 启用双因素身份验证
  • 生成应用程序密码

Gmail设置:

  • 在gmail设置中启用IMAP

名字可能会有点不同,因为我没有使用英语版的谷歌,所以“名字”是我的翻译...

相关问题