无法使用 imap 连接到 Gmail
cannot connect to Gmail using imap
我正在尝试使用 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 }
你很高兴,剩下的代码是正确
您可能还必须在帐户上允许 "less secure apps",请参阅 https://myaccount.google.com/lesssecureapps
我正在尝试使用 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 }
你很高兴,剩下的代码是正确
您可能还必须在帐户上允许 "less secure apps",请参阅 https://myaccount.google.com/lesssecureapps