0
votes

I'm currently using the nodejs package imap-simple to check an email is sent.

I've copied and pasted the relevant code from the imap-simple site to firstly delete existing emails.

The code is as follows;

 
var config = {
    imap: {
        user: '[email protected]',
        password: 'yourpassword',
        host: 'imap.gmail.com',
        port: 993,
        tls: true,
        authTimeout: 3000
    }
};
imaps.connect(config).then(function (connection) {        
    connection.openBox('INBOX').then(function () {
    
        var searchCriteria = ['ALL'];
        var fetchOptions = { bodies: ['TEXT'], struct: true };
        return connection.search(searchCriteria, fetchOptions);
 
    //Loop over each message
    }).then(function (messages) {
        let taskList = messages.map(function (message) {
            return new Promise((res, rej) => {
                var parts = imaps.getParts(message.attributes.struct); 
                parts.map(function (part) {
                    return connection.getPartData(message, part)
                    .then(function (partData) {
                        
                        //Display e-mail body
                        if (part.disposition == null && part.encoding != "base64"){
                            console.log(partData);
                        }
 
                        //Mark message for deletion
                        connection.addFlags(message.attributes.uid, "\Deleted", (err) => {
                            if (err){
                                console.log('Problem marking message for deletion');
                                rej(err);
                            }
 
                            res(); //Final resolve
                        })
                    });
                });
            });    
        })
 
        return Promise.all(taskList).then(() => {
            connection.imap.closeBox(true, (err) => { //Pass in false to avoid delete-flagged messages being removed
                if (err){
                    console.log(err);
                }
            });
            connection.end();
        });
    });
});

However, it appears to be failing and when it fails the following error is recorded;

(node:8063) UnhandledPromiseRejectionWarning: Error: read ECONNRESET at TCP.onStreamRead (internal/stream_base_commons.js:111:27) (node:8063) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:8063) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I'm not really sure why this unhandled error is being created (as the code has been pasted from the imap-simple site - apart from the user credentials of course).

I've read elsewhere that I probably need to add a catch somewhere, but I've never used promises before so some guidance as to where I need to add these catch commands within the imap-simple code would be greatly appreciated.

1

1 Answers

1
votes

If the connection to the imap server itself failes, then you might want to check your config and authentication. In any case to catch the errors you can use the following:

imaps.connect(config).then(function (connection) {  
    ....
})
.then(....)
.catch((e) => {}) //handle error here

Since you are using .then() you can simply chain a .catch() as you would a .then(). This works if you have multiple .then() chained as well, however for your nested async calls you might want to add another .catch() in the same way.