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

使用nodemailer在本地发送邮件

时间:2023-04-03 10:08:00 Node.js

本文探讨如何使用nodemailer库在本地使用API??发送邮件。安装nodemailer官网npminstallnodemailer-S基本实现//@dochttps://nodemailer.com/constnodemailer=require("nodemailer");asyncfunctionmain(){lettransporter=nodemailer.createTransport({//支持465port:587,secure:false,//truefor465,falseforotherports//port:465,//secure:true,//servicesource:163mailboxservice:"163",auth:{user:"frost@163.com",//邮箱SMTP授权码,很重要,相当于普通邮箱密码//@questionhttps://note.youdao.com/ynoteshare1/index.html?id=f9fef46114fb922b45460f4f55d96853&type=notepass:"xxx",},});letinfo=awaittransporter.sendMail({from:'"frost"',//发件人地址to:"xx@163.com,xxx@qq.com",//收件人列表主题:"Hello?",//主题行html:"Helloworld2?",//htmlbody});console.log("Messagesent:%s",info.messageId);}main().catch(console.error);直接运行nodeindex.js,去邮箱查看邮件。这里的邮箱SMTP授权码需要在对应的邮箱设置中获取。163邮箱——如何开通客户端授权码?添加附件constpath=require("path");asyncfunctionmain(){letinfo=awaittransporter.sendMail({//...附件:[{文件名:“hello.txt”,内容:“helloworld”,},{文件名:“network_image.jpg”,href:"https://img0.baidu.com/it/u=3212920999,1827079299&fm=26&fmt=auto&gp=0.jpg",},{文件名:"local.txt",路径:path.resolve(__dirname,"local.txt"),},],});}第三个文件是在本地新建一个txt文件,发现已经带上附件了。更多HTML模板-MJML文档