节点 js 服务器设置正确实施仍然我收到错误,没有连接到它(nodemailer SMTP 方法)
node js server is setup correctly implemented still I am getting error that there's no connection to it(nodemailer SMTP method)
我用 node js 设置了一个服务器来发送电子邮件到我的 gmail,虽然服务器代码是正确的并且所有的包都安装了(cors,body-parser,express)
我仍然收到没有连接的错误
错误:
服务器代码:
const express=require('express')
const app =express();
const bodyParser=require('body-parser')
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
const cors=require('cors')
app.use(cors())
const nodemailer = require('nodemailer');
function sendEmail(link,email){
const transporter=nodemailer.createTransport({
service:'gmail',
auth:{
user:'mai.sa2sa2@gmail.com',
pass:''
}
})
// send mail with defined transport object
let info = transporter.sendMail({
from: '"Fred Foo " <foo@example.com>', // sender addressn
to: email, // list of receivers
subject: "qr Link", // Subject line
text: link, // plain text body
html: link, // html body
});
console.log("Message sent: %s", info.messageId);
}
app.post("/api/sendMail",(req, res)=>{
console.log(req.body);
sendEmail(req.body.link,req.body.email)
})
app.listen(8080,()=>{
console.log("server is running ");
})
这是发送电子邮件的函数:
sendToServer(email,score){
axios({method: "POST",
url:"http://localhost:8080/api/sendMail",
data: {score:this.state.score,email:"mai.sa2sa2@gmail.com"
}}).then((response)=>{
if (response.data.status === 'success'){
alert("Message Sent.");
this.resetForm()
}else if(response.data.status === 'fail'){
alert("Message failed to send.")
}
})
}
VS 给出“缺少脚本”的代码:
所以我猜测包含代码的文件称为 app.js 或 index.js 打开 package.json 文件并将其添加到脚本部分
{
"server": "node src/server.js" // or whateverfilename you have for you node code "node src/filename.js"
}
尝试执行node server.js
启动服务器
我用 node js 设置了一个服务器来发送电子邮件到我的 gmail,虽然服务器代码是正确的并且所有的包都安装了(cors,body-parser,express) 我仍然收到没有连接的错误
错误:
服务器代码:
const express=require('express')
const app =express();
const bodyParser=require('body-parser')
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
const cors=require('cors')
app.use(cors())
const nodemailer = require('nodemailer');
function sendEmail(link,email){
const transporter=nodemailer.createTransport({
service:'gmail',
auth:{
user:'mai.sa2sa2@gmail.com',
pass:''
}
})
// send mail with defined transport object
let info = transporter.sendMail({
from: '"Fred Foo " <foo@example.com>', // sender addressn
to: email, // list of receivers
subject: "qr Link", // Subject line
text: link, // plain text body
html: link, // html body
});
console.log("Message sent: %s", info.messageId);
}
app.post("/api/sendMail",(req, res)=>{
console.log(req.body);
sendEmail(req.body.link,req.body.email)
})
app.listen(8080,()=>{
console.log("server is running ");
})
这是发送电子邮件的函数:
sendToServer(email,score){
axios({method: "POST",
url:"http://localhost:8080/api/sendMail",
data: {score:this.state.score,email:"mai.sa2sa2@gmail.com"
}}).then((response)=>{
if (response.data.status === 'success'){
alert("Message Sent.");
this.resetForm()
}else if(response.data.status === 'fail'){
alert("Message failed to send.")
}
})
}
VS 给出“缺少脚本”的代码:
所以我猜测包含代码的文件称为 app.js 或 index.js 打开 package.json 文件并将其添加到脚本部分
{
"server": "node src/server.js" // or whateverfilename you have for you node code "node src/filename.js"
}
尝试执行node server.js
启动服务器