0

In our project, we are using 'imap' node module to connect to ms office outlook mail server and also we are doing many operations like fetch, getBoxes, search, setFlags etc using imap module.

Our server is trying to perform the above actions on mail server on per minute basis and there are many parallel flows are running to do so. Parallel flows are nothing but executing above actions parallely on imap server.

But after sometime all the flows starts returning 401 which imap module is returning. Our code is nearly like below:

var Imap = require('imap')
var imap = new Imap({
    user: 'xxx@xx.com',
    password: 'xxxx',
    host: 'outlook.office365.com',
    port: 993,
    tls: true
});

imap.once('ready', function () {
  imap.openBox('inbox', false, function (err, box) {
    if (err) throw err;
    var msgno = 1;
    let fetchCheck = imap.seq.fetch(msgno + ':*', { bodies: [''] });
    fetchCheck.on('message', function (msg, seqno) {
      if (seqno == msgno) {
        imap.seq.setFlags(seqno, 'Seen', function (err) {
          if (err) {
            return err;
          } else {
            imap.end();
          }
        });
      }
    })
    fetchCheck.once('error', function (err) {
      imap.end();
      imap.closeBox();
    });
  });
});
imap.once('error', function (err) {
  console.log(err);
});
imap.once('end', function () {
  console.log('Done flagging message');
  console.log('Connection ended');
});
imap.connect();

Like above code, we are also using the search, fetch, move actions to perform on mail server and all these actions are running in parallel. So after sometime, suddenly these parallel flows starts giving 401 Error: Timed out while authenticating with server.

Is there any connection restrictions to the office mail server or the limitation of 'imap' node module on the connection handling?

Mrunal
  • 1
  • 401 isn't an IMAP error at all. IMAP errors are called OK, NO and BAD, so whatever you get isn't from an IMAP connection (and outlook.office365.com port 993 is IMAP, not gateway of any kind). You'll probably want to enable a little more debug logging and see what actually is happening. – arnt Nov 25 '19 at 10:05
  • This is the error I am getting from imap node-module: Error: Timed out while authenticating with server 2019-11-25T15:51:07.70+0530 [APP/PROC/WEB/0] OUT at Timeout. (/home/vcap/app/node_modules/imap/lib/Connection.js:139:17) 2019-11-25T15:51:07.70+0530 [APP/PROC/WEB/0] OUT at Timeout._onTimeout (/home/vcap/app/node_modules/async-listener/glue.js:188:31) 2019-11-25T15:51:07.70+0530 [APP/PROC/WEB/0] OUT at ontimeout (timers.js:498:11) 2019-11-25T15:51:07.70+0530 [APP/PROC/WEB/0] OUT at tryOnTimeout (timers.js:323:5) – Mrunal Nov 25 '19 at 10:22
  • In other words, the connection timed out. Something is discarding your packets and you need to find out what and why. – arnt Nov 25 '19 at 13:47

0 Answers0