当前位置: 首页 > 后端技术 > Node.js

使用节点发送邮件

时间:2023-04-03 23:43:01 Node.js

使用node.js配合QQ域名邮箱完成邮件发送。配置域名邮箱。详情请查看QQ邮箱。在账户项中,勾选启用SMTP服务获取密码。安装节点邮件模块installnpminstallnodemailernodemailer-smtp-transport--saveImportmoduleconstnodemailer=require('nodemailer');constsmtpTransport=require('nodemailer-smtp-transport');具体实现代码//导入邮件模块varnodemailer=require('nodemailer');varsmtpTransport=require('nodemailer-smtp-transport');//开启一个SMTP连接池vartransport=nodemailer.createTransport(smtpTransport({host:"smtp.qq.com",//qq邮箱主机secure:true,//使用SSLsecureConnection:true,//使用SSLport:465,//SMTPportauth:{user:"noreply@binlive.cn",//accountyourcustomdomainnameemailaccountpass:"olncjtfcim1232312ahj"//password你打开SMPT获取密码}}));//发送邮件路由router.post('/api/email',function(req,res){varquery=req.body.email;//设置邮件内容为拼接html美化发送内容lethtmlcon=''+''+'BinLive

'+'完成注册'+'
'+'
';varmailOptions={from:"noreply@binlive.cn",//发送地址:req.body.email,//收件人列表subject:"BinLiveaccountregistration",//titletext:"",html:htmlcon//htmlcontent}//发送邮件varuserobj={email:req.body.email,passworld:req.body.passworld,hash:hash,isregister:false}transport.sendMail(mailOptions,function(error,response){if(error){console.log("fail:"+error);console.log("发送失败");res.json({data:1})}else{console.log("发送成功");res.json({data:0})}transport.close();//如果不使用,关闭连接池});})