节点IMAP连接ECONNREFUSED 127.0.0.1:143 [英] Node IMAP connect ECONNREFUSED 127.0.0.1:143
问题描述
我将NodeJ与imap,mailparser和bluebird一起使用来读取我的gmail帐户的邮件,最后将它们写入Azure数据库.但这是我了解它如何工作的第一步.
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.
我使用来自使用node js读取电子邮件正文的代码imap 并更改凭据.
I use the code from Read email body with node js imap and alter the credentials.
当我执行email.js脚本时,它返回以下错误.
when i execute the email.js script it returns the below error.
希望您能在这里为我提供帮助.
I hope you can help me out here.
保姆,谢谢,
Erik
错误
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();
});
}
推荐答案
该代码似乎将用于连接到IMAP服务器的设置"与处理IMAP的类的实际实例混淆了.从关于如何使用node-imap
的上游示例:
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
});
您的代码在做什么,但是,首先创建IMAP连接,如示例所示:
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
});
...然后尝试使用该IMAP连接作为另一个连接的参数源:
...and then it tries to use that IMAP connection as a source of parameters for another connection:
var imap = new Imap(imapConfig);
我只是猜测node-imap
库是有用的",因为远程服务器IMAP的默认hostname
为127.0.0.1
,其余的只是编程环境中没有类型信息的结果
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.
这篇关于节点IMAP连接ECONNREFUSED 127.0.0.1:143的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!