我正尝试第一次将nodemailer用于联系表单,并且让它无法正常工作。看来,不管我做什么,都会收到以下错误:
Error: Mail command failed: 550-Requested action not taken: mailbox unavailable
550 Sender address is not allowed.
从搜寻Internet看来,这似乎与我要接收拒绝电子邮件消息的帐户有关。问题是我正在努力寻找一个明确的解决方案,以解决如何正确处理此问题,或者如何确保这确实是问题所在。
我的一般假设是,我只是不知道某个或几个东西,因为我通常对nodemailer和smtp来说是一个菜鸟。我将在下面列出所有相关的代码片段,在此先感谢您的任何有用提示。以下代码(cred data和mail.to)中的个人信息已被删除。
react组件中的前端提交功能:
handleSubmit = (event) => {
event.preventDefault();
const name = this.state.name;
const email = this.state.email;
const message = this.state.message;
axios({
method: 'POST',
url: 'http://localhost:4000/send',
data: {
name: name,
email: email,
message: message
}
}).then((response) => {
if (response.data.msg === 'success') {
alert('Message sent');
this.resetForm();
} else if (response.data.msg === 'fail') {
alert('Message failed to send.');
}
})
};
后端路由和nodemailer代码:
const express = require('express');
const router = express.Router();
const nodemailer = require('nodemailer');
const cors = require('cors');
const creds = require('./config');
const transport = {
host: 'smtp.mail.com',
port: 587,
auth: {
user: creds.USER,
pass: creds.PASS
}
}
const transporter = nodemailer.createTransport(transport);
transporter.verify((error, success) => {
if (error) {
console.log(error);
} else {
console.log('Server is ready for messages');
}
});
router.post('/send', (request, response, next) => {
const name = request.body.name;
const email = request.body.email;
const message = request.body.message;
const content = `name: ${name} \n email: ${email} \n message: ${message}`;
const mail = {
from: name,
to: 'MY GMAIL ACCOUNT WHERE I WANT TO RECIEVE THE MESSAGE',
subject: 'New message from tyleranyan.com',
text: content
};
transporter.sendMail(mail, (error, data) => {
if (error) {
response.json({
msg: 'fail'
});
console.log(error);
} else {
response.json({
msg: 'success'
});
}
});
});
const app = express();
app.use(cors());
app.use(express.json());
app.use('/', router);
app.listen(4000);
凭据的配置文件:
module.exports = {
USER: 'THE EMAIL THAT IS SENDING THE MESSAGE, IN THIS CASE A MAIL.COM EMAIL ADDRESS',
PASS: 'PASSWORD FOR SENDER EMAIL'
}
答案 0 :(得分:0)
所以,这似乎都集中在我正在使用的发件人电子邮件以及smtp地址有些奇怪的地方。
这是一个奇怪的一次性,但它的价值是:mail.com允许您创建带有自定义域的免费电子邮件帐户。因此,在这种情况下,我遇到了类似的问题:“ myName@mindless.com”和“ smtp.mail.com” smtp,这简直是行不通的。我创建了第二个gmail帐户,启用了安全性较差的应用,更新了信用信息和smtp地址,并且现在可以正常使用了。