2
這是Nodemailer的一個基本示例。無法使用Nodemailer發送郵件
var http = require('http');
var port = process.env.PORT || 8080;
var async = require('async');
var nodemailer = require('nodemailer');
// create reusable transporter object using SMTP transport
var mailOptions = {
from: '*********', // sender address
to: '[email protected]', // list of receivers
subject: 'Hello ✔', // Subject line
text: 'Hello world ✔', // plaintext body
html: '<b>Hello world ✔</b>', // html body
attachments: [
{ // utf-8 string as an attachment
filename: 'text1.txt',
content: 'hello world!'
}]
};
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: '*******',
pass: '****'
}
});
// send mail with defined transport object
var server = http.createServer(function(request, response) {
if (request.url === '/favicon.ico') {
response.writeHead(200, {'Content-Type': 'image/x-icon'});
response.end();
return;
}
response.writeHead(200, {
"Content-Type": "text/plain"
});
transporter.sendMail(mailOptions, function(error, info){
if(error){
console.log("error is " ,error);
}else{
console.log('Message sent: ' + info.response);
}
});
response.end("Hello World\n");
}).listen(port);
console.log("Node server listening on port " + port);
我收到以下錯誤要去爲localhost:
[Error: No transport method defined]
我使用Nodemailer 1.4.23版本的Windows 7可能是什麼問題呢?
也許嘗試'服務:'gmail''和/或嘗試在HTTP服務器請求之外發送電子郵件。把它歸結爲基本的例子。 – 2014-09-25 06:02:59
謝謝。編寫'gmail'解決了這個問題。他們的文檔提到了Gmail。將在Github上通知他們。如果你寫出答案,我會接受。它可能在未來幫助某人。 – rishiag 2014-09-25 06:20:36
我也注意到第一個例子有'Gmail',而另一個例子有'gmail'。很高興幫助。 – 2014-09-25 06:22:19