Skip to content

node邮箱发送

前提:进入邮箱,开启服务 生成授权码

// 开启一个 SMTP 连接池
    var transport = nodemailer.createTransport(smtpTransport({
        host: "smtp.qq.com", // 主机
        secure: true, // 使用 SSL
        port: 465, // SMTP 端口
        auth: {
            user: "你的账号", // 账号
            pass: "授权码" // 密码
        }
    }));
    
    // 设置邮件内容
    var mailOptions = {
        from: "MRMAO <mailxiami@foxmail.com>", // 发件地址
        to: "收件人邮箱", // 收件列表
        subject: "验证码", // 标题
        html: `<b>你好</b>` // html 内容
    };
// 发送邮件
    transport.sendMail(mailOptions, function(error, response) {
        if (error) {
            console.error(error);
        } else {
            console.log(response);
        }
        transport.close(); // 如果没用,关闭连接池
    });