我想用localhost smtp(Mercury Mail)配置在localhost上运行的express项目。 我已经配置了以下代码:
var express = require('express');
var router = express.Router();
var nodemailer = require('nodemailer');
/* GET users listing. */
router.get('/', function(req, res, next) {
res.render('contact', { title : 'Contact us'});
});
router.post('/send', function(req, res) {
// create reusable transporter object using the default SMTP transport
var transporter = nodemailer.createTransport("SMTP", {
host: 'localhost',
port: 25,
secure: false, // true for 465, false for other ports
auth: {
user: 'piyush@localhost', // generated ethereal user
pass: '123456' // generated ethereal password
}
});
// setup email data with unicode symbols
var mailOptions = {
from: 'piyush', // sender address
to: 'piyush@localhost', // list of receivers
subject: 'Hello', // Subject line
text: 'Hello world?', // plain text body
//html: '<b>Hello world?</b>' // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
// Preview only available when sending through an Ethereal account
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
});
});
module.exports = router;
,但在正确配置后仍然无法正常工作。我对此进行了过多搜索,但每个示例都使用SMTP提供程序(例如Gmail,Hotmail等)。