question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Connection to server is not possible

See original GitHub issue

Hi, I’m trying to recover an email from Gmail account but I can’t because the method once in the state ready is never in that state. Here is the code:

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

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

        console.log("*-*-*-*-*-* Starting email process");

        function openInbox(cb) {
            //Opens a specific mailbox that exists on the server
            console.log("..Opening INBOX..");
            imap.openBox('INBOX', true, cb);
        }

        imap.connect();
        //sleep.sleep(5);
        console.log(imap);
        
        imap.once('ready', function() {
            console.log("Entering once method");

The problem is the method once(ready) is never ready I don’t know why. Here is the console output:

Connection {
  domain: null,
  _events: {},
  _eventsCount: 0,
  _maxListeners: undefined,
  _config:
   { localAddress: undefined,
     socket: undefined,
     socketTimeout: 0,
     host: 'imap.gmail.com',
     port: 993,
     tls: true,
     tlsOptions: undefined,
     autotls: undefined,
     user: 'xyz@gmail.com',
     password: 'xyz',
     xoauth: undefined,
     xoauth2: undefined,
     connTimeout: 10000,
     authTimeout: 5000,
     keepalive: true },
  _sock:
   TLSSocket {
     _tlsOptions:
      { pipe: undefined,
        secureContext: [Object],
        isServer: false,
        requestCert: true,
        rejectUnauthorized: true,
        session: undefined,
        NPNProtocols: undefined,
        ALPNProtocols: undefined,
        requestOCSP: undefined },
     _secureEstablished: false,
     _securePending: false,
     _newSessionPending: false,
     _controlReleased: true,
     _SNICallback: null,
     servername: null,
     npnProtocol: null,
     alpnProtocol: null,
     authorized: false,
     authorizationError: null,
     encrypted: true,
     _events:
      { close: [Function],
        end: [Object],
        finish: [Function: onSocketFinish],
        _socketEnd: [Function: onSocketEnd],
        secureConnect: [Object],
        connect: [Object],
        secure: [Function],
        error: [Function],
        timeout: [Function],
        readable: [Function] },
     _eventsCount: 10,
     connecting: true,
     _hadError: false,
     _handle:
      TLSWrap {
        bytesRead: 0,
        _externalStream: {},
        fd: -1,
        _parent: [Object],
        _parentWrap: [Object],
        _secureContext: [Object],
        reading: true,
        owner: [Circular],
        onread: [Function: onread],
        writeQueueSize: 1,
        onhandshakestart: [Function],
        onhandshakedone: [Function],
        onocspresponse: [Function],
        onerror: [Function] },
     _parent:
      Socket {
        connecting: true,
        _hadError: false,
        _handle: [Object],
        _parent: null,
        _host: 'imap.gmail.com',
        _readableState: [Object],
        readable: false,
        domain: null,
        _events: [Object],
        _eventsCount: 7,
        _maxListeners: undefined,
        _writableState: [Object],
        writable: true,
        allowHalfOpen: false,
        destroyed: false,
        _bytesDispatched: 0,
        _sockname: null,
        _pendingData: null,
        _pendingEncoding: '',
        server: null,
        _server: null,
        _idleNext: null,
        _idlePrev: null,
        _idleTimeout: -1 },
     _host: null,
     _readableState:
      ReadableState {
        objectMode: false,
        highWaterMark: 16384,
        buffer: [Object],
        length: 0,
        pipes: null,
        pipesCount: 0,
        flowing: null,
        ended: false,
        endEmitted: false,
        reading: false,
        sync: true,
        needReadable: true,
        emittedReadable: false,
        readableListening: true,
        resumeScheduled: false,
        defaultEncoding: 'utf8',
        ranOut: false,
        awaitDrain: 0,
        readingMore: false,
        decoder: null,
        encoding: null },
     readable: true,
     domain: null,
     _maxListeners: undefined,
     _writableState:
      WritableState {
        objectMode: false,
        highWaterMark: 16384,
        needDrain: false,
        ending: false,
        ended: false,
        finished: false,
        decodeStrings: false,
        defaultEncoding: 'utf8',
        length: 0,
        writing: false,
        corked: 0,
        sync: true,
        bufferProcessing: false,
        onwrite: [Function: bound onwrite],
        writecb: null,
        writelen: 0,
        bufferedRequest: null,
        lastBufferedRequest: null,
        pendingcb: 0,
        prefinished: false,
        errorEmitted: false,
        bufferedRequestCount: 0,
        corkedRequestsFree: [Object] },
     writable: true,
     allowHalfOpen: false,
     destroyed: false,
     _bytesDispatched: 0,
     _sockname: null,
     _pendingData: null,
     _pendingEncoding: '',
     server: undefined,
     _server: null,
     ssl:
      TLSWrap {
        bytesRead: 0,
        _externalStream: {},
        fd: -1,
        _parent: [Object],
        _parentWrap: [Object],
        _secureContext: [Object],
        reading: true,
        owner: [Circular],
        onread: [Function: onread],
        writeQueueSize: 1,
        onhandshakestart: [Function],
        onhandshakedone: [Function],
        onocspresponse: [Function],
        onerror: [Function] },
     _requestCert: true,
     _rejectUnauthorized: true },
  _tagcount: 0,
  _tmrConn:
   Timeout {
     _called: false,
     _idleTimeout: 10000,
     _idlePrev:
      TimersList {
        _idleNext: [Circular],
        _idlePrev: [Circular],
        _timer: [Object],
        _unrefed: false,
        msecs: 10000 },
     _idleNext:
      TimersList {
        _idleNext: [Circular],
        _idlePrev: [Circular],
        _timer: [Object],
        _unrefed: false,
        msecs: 10000 },
     _idleStart: 14034,
     _onTimeout: [Function],
     _timerArgs: undefined,
     _repeat: null },
  _tmrKeepalive: undefined,
  _tmrAuth: undefined,
  _queue: [],
  _box: undefined,
  _idle: { started: undefined, enabled: false },
  _parser:
   Parser {
     domain: null,
     _events:
      { untagged: [Function],
        tagged: [Function],
        body: [Function],
        continue: [Function],
        other: [Function] },
     _eventsCount: 5,
     _maxListeners: undefined,
     _stream:
      TLSSocket {
        _tlsOptions: [Object],
        _secureEstablished: false,
        _securePending: false,
        _newSessionPending: false,
        _controlReleased: true,
        _SNICallback: null,
        servername: null,
        npnProtocol: null,
        alpnProtocol: null,
        authorized: false,
        authorizationError: null,
        encrypted: true,
        _events: [Object],
        _eventsCount: 10,
        connecting: true,
        _hadError: false,
        _handle: [Object],
        _parent: [Object],
        _host: null,
        _readableState: [Object],
        readable: true,
        domain: null,
        _maxListeners: undefined,
        _writableState: [Object],
        writable: true,
        allowHalfOpen: false,
        destroyed: false,
        _bytesDispatched: 0,
        _sockname: null,
        _pendingData: null,
        _pendingEncoding: '',
        server: undefined,
        _server: null,
        ssl: [Object],
        _requestCert: true,
        _rejectUnauthorized: true },
     _body: undefined,
     _literallen: 0,
     _literals: [],
     _buffer: '',
     _ignoreReadable: false,
     debug: undefined,
     _cbReadable: [Function] },
  _curReq: undefined,
  delimiter: undefined,
  namespaces: undefined,
  state: 'disconnected',
  debug: undefined,
  _onError: [Function],
  _onSocketTimeout: [Function] }

thanks for your help.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
hadeskuncommented, Jun 16, 2017

The output is this:

[connection] Connected to host
<= '* OK Gimap ready for requests from 137.101.66.82 e49mb34640318wre'
=> 'A0 CAPABILITY'
<= '* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH2 AUTH=PLAIN AUTH=PLAIN-CLIENTTOKEN AUTH=OAUTHBEARER AUTH=XOAUTH'
<= 'A0 OK Thats all she wrote! e49mb34640318wre'
=> 'A1 LOGIN "xxxxxxx@gmail.com" "xxxxxxxx"'
<= '* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE ENABLE MOVE CONDSTORE ESEARCH UTF8=ACCEPT LIST-EXTENDED LIST-STATUS LITERAL- SPECIAL-USE APPENDLIMIT=35651584'
<= 'A1 OK xxxxxx@gmail.com authenticated (Success)'
=> 'A2 NAMESPACE'
<= '* NAMESPACE (("" "/")) NIL NIL'
<= 'A2 OK Success'
=> 'A3 LIST "" ""'
<= '* LIST (\\Noselect) "/" "/"'
<= 'A3 OK Success'
=> 'IDLE IDLE'
<= '+ idling'

but I cann’t open the mailbox using this way:

imapObject.openBox("INBOX",true,function(err, box) {
    if (err) {
        console.error(err);
        return;
    }
    console.log("Begin the searching...");
    imapObject.search(['UNSEEN', ['FROM', order_id]], function (err, results) {
        if (!results || !results.length) {
            console.log(box.messages.new.toString() + "  " + "new messages");
            console.log("No mails");
            imapObject.end();
            return;
        }
        var f = imapObject.fetch(results, {bodies: 'TEXT'});
        f.on("message", processMessage);
        f.once("error", function (err) {
            return err;

        });
    });
});

the imapObject is just like this:

var Imap = require('imap'),
          inspect = require('util').inspect;
var imap = new Imap({
      user: 'xxxxxx@gmail.com',
      password: 'xxxxxxx',
      host: 'imap.gmail.com',
      port: 993,
      tls: true,
      debug: console.log
  });
imap.connect();
global.imapObject = imap;

please anyone know whats going on because the result in the console is just:

=> DONE

I don’t understand what’s happening?? Please help

0reactions
hareeshnkpcommented, Aug 12, 2019

Thanks… It is working for me … I assume because of synchronisation issue in my code this happen.

Read more comments on GitHub >

github_iconTop Results From Across the Web

5 Ways to Fix Unable To Connect To Server
5 Ways to Fix Unable To Connect To Server · 1. Check Internet Connection · 2. Restart Device · 3. Check Date &...
Read more >
What does "Can't Connect to Server" mean, and how to fix it?
Some software may have an issue with it, and when it requests a resource from the server, due to a software error, it...
Read more >
"Cannot connect the computer to the server" error message ...
Cannot connect the computer to the server because either another software installation is in progress or, the computer has a restart pending.
Read more >
Effective Ways To Fix "Couldn't Open Connection To Server"
Step 1: Restarting your Device · Step 2: Check if the Network Connection is working fine · Step 3: Is your SIM okay?...
Read more >
I Cannot Connect to the Server
To check that the server and network are working correctly, try to ping the server from your computer. If you cannot ping the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found