一, 创建 mail.JS 文件, 导入 Nodemailer
NPM install [email protected] // 直接 nodemailer 已经不支持了
二, 示例代码
可以参考源网站 https://nodemailer.com/about/ 给出的代码, 我这里自己精简了一下
- 'use strict';
- const nodemailer = require('nodemailer');
- // 创建发送邮件的请求对象
- let transporter = nodemailer.createTransport({
- host: 'smtp.ethereal.email',
- port: 587, // 端口号
- secure: false,
- auth: {
- user: testAccount.user, // 发送方邮箱地址
- pass: testAccount.pass // smtp 验证码
- }
- });
- // 邮件信息
- let mailObj = {
- from: '"Fred Foo ??" <[email protected]>', // 发送方邮箱及标题
- to: '[email protected], [email protected]', // 对方邮箱地址
- subject: 'Hello ?', //
- text: 'Hello world?', // 邮件内容
- html: '<b>Hello world?</b>' // HTML 格式
- };
- transporter.sendMail(mailObj);
三, 获取 host,port 和 secure
进入 node_modules --> nodemailer -- > lib --> well-known --> server.JSON
以 QQ 邮箱为例, 如果需要发送邮件的邮箱是 QQ 邮箱, 则 ctrl+f 查找 QQ, 并修改对应的 host,port 和 secure
四, 开启 POP3/SMTP 并获取授权码
同样以 QQ 邮箱为例
打开 QQ 邮箱
进入设置 --> 账户
开启 POP3/SMTP 服务
在 auth 的 user 中填入你的邮箱地址, 在 pass 中填入授权码
来源: http://www.bubuko.com/infodetail-3274319.html