您好
我正在使用nodejs Imap模块来阅读电子邮件,但从昨天开始,即2014年12月11日,我收到错误
“某些消息无法被FETCH(失败)”,
我的代码类似于
imap.once('ready', function () {
imap.openBox("[Gmail]/All Mail", true, function (err, box) {
if (err) {
d.reject(err);
}
var Moment = require("moment");
var sDate = Moment(startDate).format("MMMM DD, YYYY");
var mailSearchOptions = [];
if (params.mailSearchOptions) {
mailSearchOptions = params.mailSearchOptions
} else {
mailSearchOptions.push(['SINCE', sDate]);
if (uid) {
mailSearchOptions.push(["uid", uid + ":*"])
}
}
try {
imap.search(mailSearchOptions, function (err, results) {
if (err) {
d.reject(err);
return
}
if (!results || (Array.isArray(results) && results.length == 0)) {
d.reject(err);
return;
}
var f = imap.fetch(results, { bodies: ['HEADER.FIELDS (TO FROM SUBJECT CC)', '1', '1.1', "1.2"], struct: true });
f.on('message', function (msg, seqno) {
var mailDate = false;
var head = "";
var bodyText = {};
var struct = undefined;
msg.on("body", function (stream, info) {
stream.on('data', function (chunk) {
if (info.which == 'HEADER.FIELDS (TO FROM SUBJECT CC)') {
head += chunk.toString('utf8');
}
else {
if (bodyText[info.which] === undefined) {
bodyText[info.which] = "";
}
bodyText[info.which] += chunk.toString('utf8');
}
});
stream.once('end', function () {
if (info.which == 'HEADER.FIELDS (TO FROM SUBJECT CC)') {
head = require("imap").parseHeader(head);
}
});
});
msg.once('attributes', function (attrs) {
struct = attrs.struct;
lastUid = attrs.uid;
mailDate = attrs.date;
});
msg.once('end', function () {
if (!uid || (uid && lastUid !== parseInt(uid))) {
var bodyValue = "";
var bodyType = "";
var toPrefer = "1.1"
if ((!(bodyText[toPrefer])) || bodyText[toPrefer] == "null" || bodyText[toPrefer] == null || bodyText[toPrefer].toString().trim().length == 0) {
bodyValue = bodyText["1"];
bodyType = "1";
} else {
bodyValue = bodyText[toPrefer];
bodyType = toPrefer;
}
var encoding = getEncoding(struct, bodyType);
if (encoding && encoding == "BASE64") {
bodyValue = new Buffer(bodyValue, "base64").toString();
}
formatEmails(head);
head.subject = head.subject ? head.subject[0] : "";
var participants = [];
populateParticipants(head, username, participants);
mails.push({head: head, body: bodyValue, date: mailDate, participants: participants, user: {_id: userid, username: username}, uid: lastUid});
}
});
});
f.once('error', function (err) {
console.log("error>>>>>>>>>>>"+err) ;
d.reject(err);
});
f.once('end', function () {
if (!params.maintainLogs) {
return updateLastUid(lastUid, uid, userid, db).then(function () {
imap.end();
});
} else {
imap.end();
}
});
});
} catch (e) {
console.log("error>>>catch>>>>>>>>"+err) ;
d.reject(e);
}
});
});
imap.once('error', function (err) {
d.reject(err);
});
imap.once('end', function () {
d.resolve(mails);
});
imap.connect();
});
我检查过,如果我从
中删除'1.1'和'1.2'var f = imap.fetch(results, { bodies: ['HEADER.FIELDS (TO FROM SUBJECT CC)', '1', '1.1', "1.2"], struct: true });
然后变为
var f = imap.fetch(results, { bodies: ['HEADER.FIELDS (TO FROM SUBJECT CC)', '1'], struct: true });
不会给我错误,但通过删除它我无法获得相应的电子邮件数据
昨天之前一切正常......
请帮忙......