这里我主要使用的是 nodemailer 这个插件
第一步 下载依赖
cnpm install nodemailer --save
第二步 建立email.js
‘use strict‘;const nodemailer = require(‘nodemailer‘);let transporter = nodemailer.createTransport({ ?// host: ‘smtp.ethereal.email‘, ?service: ‘qq‘, // 使用了内置传输发送邮件 查看支持列表:https://nodemailer.com/smtp/well-known/ ?port: 465, // SMTP 端口 ?secureConnection: true, // 使用了 SSL ?auth: { ???user: ‘xxxxxx@qq.com‘,//你的邮箱 ???// 这里密码不是qq密码,是你设置的smtp授权码 ???pass: ‘qq的授权码‘, ?}});let mailOptions = { ?from: ‘"火星黑洞" <1418275530@qq.com>‘, // sender address ?to: ‘156093340@qq.com‘, // list of receivers ?subject: ‘Hello‘, // Subject line ?// 发送text或者html格式 ?// text: ‘Hello 我是火星黑洞‘, // plain text body ?html: ‘<b>Hello 我是火星黑洞</b>‘ // html body};// send mail with defined transport objecttransporter.sendMail(mailOptions, (error, info) => { ?if (error) { ???return console.log(error); ?} ?console.log(‘Message sent: %s‘, info.messageId); ?// Message sent: <04ec7731-cc68-1ef6-303c-61b0f796b78f@qq.com>});
第三步,执行 node email.js,即可发送成功~
注:可以前往 https://jingyan.baidu.com/article/fedf0737af2b4035ac8977ea.html 去查看如何设置授权码
nodejs发送邮件
原文地址:https://www.cnblogs.com/ldlx-mars/p/9757827.html