Node IMAP connect ECONNREFUSED 127.0.0.1:143

核能气质少年 提交于 2020-01-06 04:55:33

问题


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: 'myemail@gmail.com', 
  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();
    });
}

回答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: 'mygmailname@gmail.com',
  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: 'myemail@gmail.com', 
  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.



来源:https://stackoverflow.com/questions/59459630/node-imap-connect-econnrefused-127-0-0-1143

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!