在开发中,可以使用向邮箱发送邮件的形式来实现验证码或者忘记密码的功能。 这里以express框架为例。
- 安装express
npm install express
- 搭建路由
const express=require("express");
const app=express();
const router=express.Router();
app.use(router);
app.listen(8888,()=>{ //监听8888端口
console.log("服务启动")
})
3.安装 nodemailer
npm install nodemailer
- 设置访问地址 http://localhost:8888/send
const nodemailer=require("nodemailer");
router.get("/send",(req,res,next)=>{
let transport=nodemailer.createTransport({
host:"smtp.126.com", //服务器地址 在开通IMAP/SMTP后显示
secureConnection:true,
port:25,
auth:{
user:"xxxx@126.com", //自己的邮箱地址
pass:"INSXECECXITIVDNG" //邮箱的授权码
}
})
let mainOptions={
from:"xxxx@126.com", //自己的邮箱地址
to:" ccc@163.com", //收件人的邮箱地址
subject:"今天是个好日子",
text:"窗前明月光 疑似地上霜 举头望明月 低头思故乡",
html:"<div> <img src='http://www.baidu.com/moment/picture?id=1624173203660&type=small' /> </div>"
}
transport.sendMail(mainOptions,(err,response)=>{
if(err){
console.log(err)
}else{
res.json({
mesage:response
})
}
})
})
5.在浏览器访问 http://localhost:8888/send 就可以向ccc@163.com发送邮件了