标签:
废话不多说,直接上代码。。。
var Imap = require(‘imap‘);
var inspect = require(‘util‘).inspect;
var MailParser = require("mailparser").MailParser;
var nodemailer = require(‘nodemailer‘);
var fs = require(‘fs‘);
var http = require(‘http‘);
var imap = new Imap({
user: ‘youremail‘,
password: ‘yourpassword.‘,
host: ‘imap.qq.com‘,
port: 993,
tls: true
});
var smtpTransport = nodemailer.createTransport({
host: ‘smtp.qq.com‘,
port: 465,
secure: true, // use SSL
auth: {
user: ‘youremail‘,
pass: ‘yourpassword‘
}
});
function openInbox(cb) {
imap.openBox(‘INBOX‘, true, cb);
}
imap.once(‘ready‘, function() {
openInbox(function(err, box) {
if (err) throw err;
imap.on(‘mail‘, function() {
console.log("收到一封新邮件!")
//readMailbox();
http.createServer(readMailbox).listen(3100);
})
});
})
imap.once(‘error‘, function(err) {
console.log(err);
});
// imap.once(‘end‘, function() {
// console.log(‘邮件收取结束‘);
// });
imap.connect();
function readMailbox(req, res) {
//只读取新邮件
//
res.writeHead(200, { ‘Content-Type‘: ‘text/html;charset=utf-8‘ });
//new代表新邮件,未读邮件
imap.search([‘NEW‘], function(err, results) {
var count = 0;
//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) {
var mailparser = new MailParser();
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);
}
//pipe到mailparse解析器
stream.pipe(mailparser);
var buffer = ‘‘,
count = 0;
stream.on(‘data‘, function(chunk) {
count += chunk.length;
buffer += chunk.toString(‘utf8‘);
});
//stream结束,向console发出消息
stream.on(‘end‘, function() {
// if (info.which !== ‘TEXT‘) {
// console.log(prefix + ‘Parsed header: %s‘, inspect(Imap.parseHeader(buffer)));
// } else {
// console.log(prefix + ‘Body[%s] Finished‘, inspect(info.which));
// }
});
//保存邮件内容到本地
mailparser.on("end", function(mail) {
//console.log(typeof(mail))
//mail是一个对象
fs.writeFile(‘msg- ‘ + seqno + ‘.html‘, mail.html, function(err) {
if (err) {
throw err;
}
});
//如果有附件,则存储起来。
if (mail.attachments) {
mail.attachments.forEach(function(attachment) {
fs.writeFile(‘msg-‘ + seqno + ‘-‘ + attachment.generatedFileName, attachment.content, function(err) {
if (err) {
throw err;
}
});
});
};
res.end(mail.html);
// console.log(mail.from,mail.headers);
if (mail.html.indexOf("hello") != -1) {
console.log(mail.from[0].address);
var mailOptions = {
from: "46967489@qq.com",
to: mail.from[0].address,
subject: "node邮件",
html: ‘<h2>如果你的邮件中含有"hello"字样,将会自动收到这样一封邮件!</h2><p style="color:#e00;">来自李志祥的邮件</p>‘
}
smtpTransport.sendMail(mailOptions, function(err, resp) {
if (err) {
console.log(err);
}
console.log("发送成功")
smtpTransport.close(); //关闭连接池
});
}
});
});
f.on(‘error‘, function(err) {
console.log(‘Fetch error: ‘ + err);
});
// f.on(‘end‘, function() {
// count++;
// console.log(‘已抓取到‘ + count + ‘邮件‘);
// //imap.end()
// });
});
});
}
node.js实现收取邮件,以及对特定邮件内容的自动回复,邮件正文保存的功能
标签:
原文地址:http://www.cnblogs.com/lyfn/p/5372265.html