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

节点发送邮件

时间:2023-04-03 17:31:32 Node.js

windows上自动发邮件下载node到本地下载地址https://nodejs.org/dist/v12.1...添加node到环境变量使用bat脚本下载nodemailer运行nodemailer官网https://nodemailer。com/about/@echooffsetpath1=%~d0setpath2=%cd%setpath3=%0setpath4=%~dp0setpath5=%~sdp0echo当前盘符path1:%path1%echo当前路径path2:%path2%echo当前命令行path3:%path3%echo当前bat文件路径path4:%path4%echo当前bat文件短路径path5:%path5%setx"Path""%path4%node"node-vnpm-vcd%path4%npminstallnodemailer-savenode./email.jspause>nul将email.js和"usestrict"写入bat脚本同级目录;constnodemailer=require("nodemailer");//定义一个异步方法asyncfunctionmain(){//从ethereal.email生成测试SMTP服务账户//只有当你没有用于测试的真实邮件账户时才需要testAccount=awaitnodemailer.createTestAccount();//创建SMTP协议的连接对象lettransporter=nodemailer.createTransport({//连接地址host:"smtp.ethereal.email",//连接端口port:587,//如果为true,默认端口465,为false其他端口secure:false,auth:{//邮箱用户名user:testAccount.user,//邮箱密码pass:testAccount.pass}});//通过连接对象发送邮件com,baz@example.com",//Subject主题:"Hello",//文本内容text:"Helloworld?",//也可以发送htmlhtml:"Helloworld?",attachments:[{//文件名filename:'test.txt',//文件路径path:'../power/power/test.txt'}]});}main().catch(console.error);