在nodejs程序中,我们可以使用nodemailer来发送邮件
1、安装nodemailer
npm install nodemailer
2、实现代码如下
<span style="font-size:18px;"><span style="font-size:18px;">var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
host: "smtp.qq.com",
auth: {
user: 'user@qq.com',
pass: 'userpass'
}
});</span>
transporter.sendMail({
from: 'user@qq.com',
to: 'addr@qq.com',
subject: 'hello',
html:'<p>这是邮箱确认链接<a href="http://www.baidu.com">http://www.baidu.com</a></p>'
}, function(err, response){
console.log('send mail err is ', err, response);
transporter.close();
});
收到的截图为:
当然,你也可以使用163的服务,改成smtp.163.com即可。
发送者的账号需要在邮箱设置中开启POP3/STMP服务。
nodemailer支持的邮件服务器列表官网有列,https://github.com/andris9/nodemailer-wellknown#supported-services ,不过163的邮件服务没找到。。。测试过后是可以用的。