2

When I create an IMAP connection with node-imap module with a Mail-in-a-box (MIAB) email, after a few minutes, I get the ECONNRESET error. I want to know if it is a fault in the connection configuration, or is it from MIAB server side that will disconnect the connection if it has been idle for too long.

Here is my IMAP connection configuration:

 {
          port: 993,
          tls: true,
          authTimeout: 30000,
          connTimeout: 30000,
          keepalive: {
            forceNoop: true,
          },
          tlsOptions: {
            rejectUnauthorized: false,
          },
}

And this is the error I got:

Error: read ECONNRESET
    at TCP.onStreamRead (internal/stream_base_commons.js:209:20) {
  errno: -104,
  code: 'ECONNRESET',
  syscall: 'read',
  source: 'socket'
}

I am using the mail-notifier module, which is a wrapper of node-imap module. This is the code where I register for events in the IMAP connection

const notifier = Notifier(imap);
notifier
  .on("end", () => notifier.start())
  .on("connected", () => {
    console.log(`Connected IMAP: ${user}`);
  })
  .on("mail", (mail) => {
    this.onMail(mail, user);
  })
  .on("error", (e: any) => {
    console.log(`IMAP error: ${user}, time: ${Date.now()}`);
    console.log(e);
    setTimeout(() => {
      notifier.stop();
      notifier.start();
    }, 5000);
  })
  .start();
Tsuu
  • 96
  • 1
  • 6
  • 1
    Can you post the rest of your code and minimize it to demonstrate the error? There are many reasons you could get this error, but my first guess is that the connection times out and then terminates the connection resulting in a connection reset error. – kevintechie Jan 13 '22 at 07:56
  • I have updated the code above – Tsuu Jan 13 '22 at 09:37
  • 1
    In general, yes, most IMAP servers will close a connection for inactivity; usually around 10 minutes, or 30 minutes if the connection is running the `IDLE` command. Your code or the module you're using should account for that by running a `NOOP` at regular intervals. – Max Jan 13 '22 at 14:10

0 Answers0