0

I am using node-imap library to read mail, mail event is not getting triggered 2nd time after initialization.

below is my code

also, its giving Error: read ECONNRESET as error after 1st retrival of the email.

Expecting imap.once('mail', function (x) { } should invoke whenever any new mail arises in the mailbox.

imap.once('mail', function (x) { } is not getting triggered triggered.

This event triggers only once when I run the node.js file, and later it is not getting triggered. Please sugggest.

imap.connect();

imap.once('ready', function () {
    console.log("Imap ready");
    readMail();
});

function readMail() {
    openInbox(function (err, box) {

        imap.once('mail', function (x) {
            console.log("New Mail...", x);
            executeMail(err);
        });
    });
    }
}

Tried below as per the comment but, still doses not worked.

function readMail() {
    openInbox(function (err, box) {

        imap.once('mail', function (x) {
            console.log("New Mail...", x);
            executeMail(err);
            imap.connect();
        });
    });
    }
}
pushpak
  • 198
  • 2
  • 10
  • When the connection is closed, that's what ECONNRESET means, you need to run `imap.connect()` again. If you don't, that connection is closed and remains closed for the rest of eternity. – arnt Feb 12 '19 at 10:43
  • Where should I mention imap.connect() ? currently, imap.connect() executes only once when file gets initialize while node application starts!!! I have updated my question please have a look – pushpak Feb 13 '19 at 07:43
  • ECONNRESET means that the connection was reset. Nothing more will happen after that point. Whether, when and how to reconnect is up to you. – arnt Feb 13 '19 at 08:58

1 Answers1

2

I guess you should use imap.on not imap.once

imap.on("mail", mail => {
  console.log("New mail arrived 1");
});

The above code worked for me.

Prashanth S.
  • 410
  • 4
  • 19