node-imap: Timed out while authenticating with server

Hello,

Very Rarely, I come across the error “Timed out while authenticating with server”. Is there a way to handle this error gracefully? Connection event imap.once('error', function(err) {}) does not help. I am on node 0.8.27 with node-imap 0.8.14. Error details are below.

Error: Timed out while authenticating with server
 at Object._onTimeout (/app/node_modules/imap/lib/Connection.js:137:17)
 at Timer.list.ontimeout (timers.js:101:19)

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 25 (14 by maintainers)

Most upvoted comments

I am getting same error: Error: Timed out while authenticating with server at Object._onTimeout (/app/node_modules/imap/lib/Connection.js:137:17) at Timer.list.ontimeout (timers.js:101:19)

If you’re re-using the same imap instance like that, you need to change imap.once('error', ...) to imap.on('error', ...) because otherwise. Same with imap.once('ready', ...).

However if you are seeing a ready event and it’s not authenticated, that’s a bug.

Here is what I found so far. please check inline comments.

imap.once('error', function(err) {
  console.log(err);
})

imap.once('ready', function() {
  /*
  Very rarely, I come across `imap.state` is 'connected' at this point. According to docs` 'ready' event
  is emitted when a connection to the server has been made and authentication was successful.
  I suppose `imap.state` should have been 'authenticated' here.
  */
  if(imap.state != 'authenticated') { //So, I check here if the state is 'authenticated' to make sure, otherwise the execution fails at some point.
    imap.connect() //So, I try to make the connection again.
  /*
  Trying to connect again as above sometimes works.
  However if it does not work and if a "Timed out while authenticating with server" error occurs
  at this second connection attempt `imap.once('error', function(err) {})` does not catch the error.
  Instead an uncaught exception occurs.
  */
  }
})