想用node-imap实现对指定邮箱未读邮件的读取操作,在读取结束之后将该邮件标记为已读,现在读取未读邮件这一步已经实现了,但是对于如何标记为已读该怎么操作啊?
1 回复
找了半天找到解决办法了,在非只读模式下打开邮箱,然后按照API 给出的在 fetch
方法的第二个参数中添加 markseen:true
就行了,示例如下:
var Imap = require('imap'),
inspect = require('util').inspect;
var imap = new Imap({
user: '[email protected]',
password: 'XXXXX',
host: 'imap.gmail.com',
port: 993,
tls: true
});
function openInbox(cb) {
imap.openBox('INBOX', false, cb);
}
imap.once('ready', function() {
openInbox(function(err, box) {
if (err) throw err;
imap.search([ 'UNSEEN', ['SINCE', 'May 20, 2014'] ], function(err, results) {
console.log('unseen mail count: ' + results.length);
if (err) throw err;
var f = imap.fetch(results, { bodies: '', markSeen : true });
f.on('message', function(msg, seqno) {
console.log('Message #%d', seqno);
var prefix = '(#' + seqno + ') ';
msg.on('body', function(stream, info) {
if (info.which === 'TEXT')
console.log(prefix + 'Body [%s] found, %d total bytes', inspect(info.which), info.size);
var buffer = '', count = 0;
stream.on('data', function(chunk) {
count += chunk.length;
buffer += chunk.toString('utf8');
if (info.which === 'TEXT')
console.log(prefix + 'Body [%s] (%d/%d)', inspect(info.which), count, info.size);
});
stream.once('end', function() {
console.log(buffer);
if (info.which !== 'TEXT')
console.log(prefix + 'Parsed header: %s', inspect(Imap.parseHeader(buffer)));
else
console.log(prefix + 'Body [%s] Finished', inspect(info.which));
});
});
msg.once('attributes', function(attrs) {
console.log(prefix + 'Attributes: %s', inspect(attrs, false, 8));
});
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.end();
});
});
});
});
imap.once('error', function(err) {
console.log(err);
});
imap.once('end', function() {
console.log('Connection ended');
});
imap.connect();