Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot connect to Gmail using imap

I am trying to view the contents of the emails received in my gmail inbox using imap but when I'm running the code, I am getting the following error.

ERROR

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'
}

CODE

var Imap = require('imap'),
    inspect = require('util').inspect;

var imap = new Imap({
  user: '[email protected]',
  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();
like image 723
Debdut Goswami Avatar asked Jan 07 '20 17:01

Debdut Goswami


2 Answers

I had the same issue for quite a long while... The real problem here is that, there is a complaint for self signed certificate and I got a way out.
Just under your tls: true which is the self signed certificate, inside your var imap= new imap({ //inside here }) add a line:
tlsOptions: { rejectUnauthorized: false }
And you're good to go, the rest of the code is correct

like image 75
VirtualChords Avatar answered Sep 17 '22 15:09

VirtualChords


You may also have to allow "less secure apps" on the account, see https://myaccount.google.com/lesssecureapps

like image 40
TiemenSchut Avatar answered Sep 18 '22 15:09

TiemenSchut