只需四步
1.設置一個用于發送郵件的賬戶,這里以qq郵箱為例。登錄你的qq郵箱》設置
QQ截圖20180119105434.png
開啟POP3/SMTP和IMAP/SMTP 服務。開啟后你會得到,一個 POP3/SMTP密碼.
2.安裝nodemailer
npm install nodemailer --save
3.寫代碼 sendmail.js
let nodemailer = require('nodemailer');
const POP3_SMTP_PASS = "ywrv*******xymfcebgg";
let transporter = nodemailer.createTransport({
//https://github.com/andris9/nodemailer-wellknown#supported-services 支持列表
service: 'qq',
port: 465, // SMTP 端口
secureConnection: true, // 使用 SSL
auth: {
//配置 POP3_SMTP 的賬號
user: '2502****5@qq.com',
//這里密碼不是qq密碼,是你設置的smtp密碼
pass: POP3_SMTP_PASS
}
});
// NB! No need to recreate the transporter object. You can use
// the same transporter object for all e-mails
// setup e-mail data with unicode symbols
let mailOptions = {
from: '250****695@qq.com', // 發件地址
to: '8****470@qq.com', // 收件列表
subject: 'Hello sir', // 標題
//text和html兩者只支持一種
//text: 'Hello world ?', // 標題
html: '<b>Hello world ?</b> <a // html 內容
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
- 執行
node sendmail.js
如果發送成功,麻煩給個喜歡或者贊!!!!