版本信息:html
Meteor:windows MIS安裝 0.6.4node
node-imap:npm指定的0.8.0版,不是默認的0.7.x版。git
mailparser:npm安裝0.3.6github
如下是記錄踩到的坑:npm
1. 使用meteor的email來發送郵件時,設置process.env.MAIL_URL要注意,若是你的gmail帳號是本身設置了domain的,如「xxx@unitedstack.com」。那麼process.env.MAIL_URL的寫法應該是:「smtp://xxx%40unitedstack.com:YOUR_PASSWORD@smtp.googlemail.com:465」。必定注意第一個@寫成「%40」。windows
2.github上node-imap的版本和例子都是適用於0.8.0的。使用npm若是安裝默認的0.7.x版則跑不通官網例子。dom
3.windows下安裝mailparser時會出現 「MSBuild」 報出的錯誤。安裝 visual studio 而後重啓就好了。fetch
由於線上搜到的node-imap+mailparser的例子都有版本的問題。如下給出一個能在上面描述的版本中跑通的例子:ui
var Imap = require('imap') var MailParser = require("mailparser").MailParser var fs = require("fs") var imap = new Imap({ user: 'YOUR_USERNAME', password: 'YOUR_PASSWORD', host: 'imap.gmail.com', port: 993, tls: true, tlsOptions: { rejectUnauthorized: false } }); function openInbox(cb) { imap.openBox('INBOX', true, cb); } var messages = [] imap.once('ready', function() { openInbox(function(err, box) { console.log("open") if (err) throw err; imap.search([ 'UNSEEN', ['SINCE', 'May 20, 2010'] ], function(err, results) { if (err) throw err; var f = imap.fetch(results, { bodies: '' }); f.on('message', function(msg, seqno) { var mailparser = new MailParser() msg.on('body', function(stream, info) { stream.pipe( mailparser ); mailparser.on("end",function( mail ){ fs.writeFile('msg-' + seqno + '-body.html', mail.html, function (err) { if (err) throw err; console.log(seqno + 'saved!'); }); }) }); msg.once('end', function() { console.log(seqno + '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();