node.js实现发送邮件功能

准备事项

  1. QQ邮箱设置:进入QQ邮箱->设置->账户->POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务->开启POP3/SMTP服务,并复制pass秘钥
  2. 安装nodemailer :npm install nodemailer --save
  3. 基于express的node.js后台

代码编写

router.post('/sendEmails', (req,res,next)=>{
    var transport = nodemailer.createTransport({
        host: "smtp.qq.com",
        secureConnection: true,
        port: 465,
        auth: {
            user: "[email protected]",
            pass: "xxxxxxx"  //刚才
        }
    })

    var mailOptions = {
        from: "[email protected]",
        to: "[email protected]",
        subject: "一封邮件",
        text: "hello hello"
    }

    transport.sendMail(mailOptions,function(err,response){
        if(err) console.log(err)
        else console.log(response)
    })
})

猜你喜欢

转载自blog.csdn.net/qq_40816360/article/details/84800656