我的获取请求 ( ) 在侦听器内部时立即imap.seq.fetch
触发事件。'end'
imap.on('mail')
on('mail')
如果我在侦听器之外获取最新邮件,则获取请求会触发on('message')
预期的事件。
这是我正在使用的库:https ://www.npmjs.com/package/imap
这有效:
const Imap = require('imap');
const imap = new Imap({
user: '',
password: '',
host: '',
port: ,
tls:
});
function openInbox(cb) {
imap.openBox('All Mail', true, cb);
}
imap.once('ready', () => {
console.log("READY");
openInbox((err, box) => {
if (err) throw err;
const f = imap.seq.fetch(box.messages.total, {bodies: ['HEADER.FIELDS (FROM SUBJECT)', 'TEXT']});
f.on('message', function(msg, seqno) {
console.log('Message #%d', seqno);
var prefix = '(#' + seqno + ') ';
msg.on('body', function(stream, info) {
var buffer = '';
stream.on('data', function(chunk) {
buffer += chunk.toString('utf8');
});
stream.once('end', function() {
console.log(buffer);
});
});
msg.once('end', function() {
console.log(prefix + 'Finished');
});
});
f.once('error', function(err) {
console.log('Fetch error: ' + err);
});
f.once('end', function() {
console.log('Done fetching all messages!');
});
});
});
imap.once('error', function(err) {
console.log(err);
});
imap.once('end', function() {
console.log('Connection ended');
});
imap.connect();
这不起作用:
const Imap = require('imap');
const imap = new Imap({
user: '',
password: '',
host: '',
port: ,
tls:
});
function openInbox(cb) {
imap.openBox('All Mail', true, cb);
}
imap.once('ready', () => {
console.log("READY");
openInbox((err, box) => {
if (err) throw err;
imap.on('mail', (evt) => {
const f = imap.seq.fetch(box.messages.total, {bodies: ['HEADER.FIELDS (FROM SUBJECT)', 'TEXT']});
f.on('message', function(msg, seqno) {
console.log('Message #%d', seqno);
var prefix = '(#' + seqno + ') ';
msg.on('body', function(stream, info) {
var buffer = '';
stream.on('data', function(chunk) {
buffer += chunk.toString('utf8');
});
stream.once('end', function() {
console.log(buffer);
});
});
msg.once('end', function() {
console.log(prefix + 'Finished');
});
});
f.once('error', function(err) {
console.log('Fetch error: ' + err);
});
f.once('end', function() {
console.log('Done fetching all messages!');
});
});
});
});
imap.once('error', function(err) {
console.log(err);
});
imap.once('end', function() {
console.log('Connection ended');
});
imap.connect();
这是一个错误还是我做错了什么?我阅读了文档,但找不到任何原因,这不是太有效。任何帮助都非常感谢!