通过 nodemailer 发送电子邮件时出现 550 错误 [英] 550 error when sending email through nodemailer

查看:154
本文介绍了通过 nodemailer 发送电子邮件时出现 550 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次尝试将 nodemailer 用于联系表单,并且我花了很长时间才能让它工作.似乎无论我做什么,我都会收到以下错误:

I'm trying to use nodemailer for the first time for a contact form and am having a hell of a time getting this to work. It seems like no matter what I do, I get the following error:

Error: Mail command failed: 550-Requested action not taken: mailbox unavailable
550 Sender address is not allowed.

从网上搜索来看,这似乎与我想接收拒绝电子邮件的消息的帐户有某种关系.问题是我正在努力寻找一个明确的解决方案来正确处理这个问题,或者如何确保这确实是问题所在.

From scouring the internet, it seems this is somehow related to the account where I want to receive the message rejecting the email. The problem is I'm struggling to find a clear solution for how to properly handle this, or how to be sure that that is indeed the problem.

我的一般假设是有一些东西,或一些东西,我只是不知道,因为我一般对 nodemailer 和 smtp 有点菜鸟.我在下面包含了所有相关的代码片段,在此先感谢您提供有关此代码的任何有用提示.以下代码中的个人信息已被删除(creds data 和 mail.to).

My general assumption is there is something, or several somethings, that I'm just unaware of since I'm sort of a noob to nodemailer and smtp generally. I'm including all relevant code snippets below, thanks in advance for any helpful hints on this one. Personal info has been removed in the below code (creds data and mail.to).

react 组件中的前端提交功能:

Frontend submit function in react component:

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 代码:

Backend routes and nodemailer code:

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'
}

推荐答案

所以,似乎这一切都围绕着我使用的发件人电子邮件以及 smtp 地址的一些奇怪之处.

So, it seems this all centered around the sender email I was using and some weirdness with the smtp address.

这有点奇怪,但它的价值在于:mail.com 允许您使用自定义域创建免费电子邮件帐户.所以在这种情况下,我有一些类似的东西:myName@mindless.com"和一个smtp.mail.com"的smtp,这根本不起作用.我创建了第二个 Gmail 帐户,启用了安全性较低的应用程序,更新了信用信息和 smtp 地址,现在它运行良好.

It's kind of an odd one-off, but for what it's worth: mail.com allows you to create a free email account with a customized domain. So in this case, I had something along the lines of: "myName@mindless.com" with an smtp of "smtp.mail.com" and that was simply not working. I made a second gmail account, enabled less secure apps, updated the creds info and smtp address, and it's now working beautifully.

这篇关于通过 nodemailer 发送电子邮件时出现 550 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆