gpt4 book ai didi

node.js - 无法使用 imap 连接到 Gmail

转载 作者:行者123 更新时间:2023-12-03 12:16:06 24 4
gpt4 key购买 nike

我正在尝试使用 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();

最佳答案

我有很长一段时间都遇到同样的问题......这里真正的问题是,有人提示自签名证书,我找到了出路。就在您的下方 tls: true这是自签名证书,在您的 var imap= new imap({ //inside here }) 中添加一行:tlsOptions: { rejectUnauthorized: false }你很高兴去,其余的代码是正确的

关于node.js - 无法使用 imap 连接到 Gmail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59633564/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com