我用nodejs给朋友发送了一封邮件
配置 node
- 需要电脑上有node.js(官网下载即可)
- 需要用到nodemailer
// 1. 配置环境
npm init
npm install nodemailer --save
写代码
const nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport({
host: 'smtp.qq.com',
secureConnection: true,
port: 465,
secure: true,
auth: {
user: 'xxx@qq.com',
pass: 'xxxxxxxxxx'
}
});
let mailOptions = {
from: '"xxx" <xxx@qq.com>',
to: 'xxxx@qq.com',
subject: '测试邮件',
text: '这是一封来自 Node.js 的测试邮件',
html: '<b>这是一封来自 Node.js 的测试邮件</b>',
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log(`Message: ${info.messageId}`);
console.log(`sent: ${info.response}`);
});
成功后终端显示的内容
朋友收到的邮件:
|