node.js email verification code

No nonsense, just do it directly.
I use Netease mailbox.
insert image description here
We log in to the Netease mailbox on the webpage, find the above page in the settings , and then we need
lMAP/SMTP service
POP3/SMTP service
. These services are all enabled, and an email authorization code will be obtained after activation.
The next step is to download the packages we depend on on the project

npm install nodemailer -s

After the download is complete

const nodemailer = require('nodemailer');


//发送验证码
function SendVerification(emailCode) {
    
    
    return new Promise((resolve, reject) => {
    
    
        // 创建可重用邮件传输器
        const transporter = nodemailer.createTransport({
    
    
            host: "smtp.163.com", // 网易的邮件地址
            port: 465, // 端口
            secureConnection: false, // use SSL
            auth: {
    
    
                "user": '[email protected]', // 邮箱账号
                "pass": 'AFSXGSUBEIICMKXT' // 邮箱的授权码
            }
        });

        const send = (mailOptions) => {
    
    
            transporter.sendMail(mailOptions, function(error, info) {
    
    
                if (error) {
    
    
                    return console.log(error);
                }
                resolve(info.messageId)
                console.log('Message send: %s', info.messageId);
            });
        }
        let email = {
    
    
            title: '见证码',
            htmlBody: '<h1>Hello!</h1><p style="font-size: 18px;color:#000;">在线的验证码为:<u style="font-size: 16px;color:#1890ff;">' +
                emailCode + '</u></p><p style="font-size: 14px;color:#666;">10分钟内有效</p>'
        }
        let mailOptions = {
    
    
            from: '[email protected]', // 发件人地址
            to: '[email protected],[email protected]', // 收件人地址,多个收件人可以使用逗号分隔
            subject: email.title, // 邮件标题
            html: email.htmlBody // 邮件内容
        };
        send(mailOptions)
    })
}

Then it's time to use it.

SendVerification('随机的动态码').then((res) =>{
    
    
   console.log('发送成功!'); 
})

Watch: https://www.cnblogs.com/xinsir/p/10256310.html
If you think it’s good, give it a like

Guess you like

Origin blog.csdn.net/weixin_44655037/article/details/121669768