Node IMAP connect ECONNREFUSED 127.0.0.1:143
Asked Answered
G

1

0

I am using NodeJs together with imap, mailparser and bluebird to read the mail of my gmail account and finally write these to an Azure Database. But this is the first step for me to get a feeling how this works.

I use the code from Read email body with node js imap and alter the credentials.

when i execute the email.js script it returns the below error.

I hope you can help me out here.

Manny thanks,

Erik

ERROR

Connection error: Error: connect ECONNREFUSED 127.0.0.1:143
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1129:14)

email.js

var Imap = require("imap");
var MailParser = require("mailparser").MailParser;
var Promise = require("bluebird");
Promise.longStackTraces();

// Step 2: Declaring new imap object
var imapConfig = new Imap({
  user: '[email protected]', 
  password: 'mypassword', 
  host: 'imap.gmail.com', 
  port: 993,
  tls: true,
  secure: true
});

var imap = new Imap(imapConfig);
Promise.promisifyAll(imap);

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

imap.once("ready", execute);
imap.once("error", function(err) {
    console.error("Connection error: " + err.stack);
});

imap.connect();

function execute() {
    imap.openBox("INBOX", false, function(err, mailBox) {
        if (err) {
            console.error(err);
            return;
        }
        imap.search(["UNSEEN"], function(err, results) {
            if(!results || !results.length){console.log("No unread mails");imap.end();return;}
            var f = imap.fetch(results, { bodies: "" });
            f.on("message", processMessage);
            f.once("error", function(err) {
                return Promise.reject(err);
            });
            f.once("end", function() {
                console.log("Done fetching all unseen messages.");
                imap.end();
            });
        });
    });
}


function processMessage(msg, seqno) {
    console.log("Processing msg #" + seqno);
    // console.log(msg);

    var parser = new MailParser();
    parser.on("headers", function(headers) {
        console.log("Header: " + JSON.stringify(headers));
    });

    parser.on('data', data => {
        if (data.type === 'text') {
            console.log(seqno);
            console.log(data.text);  /* data.html*/
        }
     });

    msg.on("body", function(stream) {
        stream.on("data", function(chunk) {
            parser.write(chunk.toString("utf8"));
        });
    });
    msg.once("end", function() {
        // console.log("Finished msg #" + seqno);
        parser.end();
    });
}
Grandioso answered 23/12, 2019 at 18:13 Comment(0)
C
1

The code appears to confuse "settings to use to connect to an IMAP server" with an actual instance of a class which handles IMAP. From the upstream example on how to use node-imap:

var Imap = require('imap'),
    inspect = require('util').inspect;

var imap = new Imap({
  user: '[email protected]',
  password: 'mygmailpassword',
  host: 'imap.gmail.com',
  port: 993,
  tls: true
});

What you code is doing, however, first creates the IMAP connection as in the example:

var imapConfig = new Imap({
  user: '[email protected]', 
  password: 'mypassword', 
  host: 'imap.gmail.com', 
  port: 993,
  tls: true,
  secure: true
});

...and then it tries to use that IMAP connection as a source of parameters for another connection:

var imap = new Imap(imapConfig);

I'm only guessing that the node-imap library is "helpful" by having a default hostname for the remote server IMAP of 127.0.0.1, and the rest is just a result of no type information in your programming environment.

Carnival answered 2/1, 2020 at 15:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.