如何解决 code: 'EAUTH', response: '535-5.7.8 Username and Password not accepted.responseCode: 535, command: 'AUTH PLAIN'

How to solve code: 'EAUTH', response: '535-5.7.8 Username and Password not accepted.responseCode: 535, command: 'AUTH PLAIN'

我的网站上有一个与我联系的表单,我正在尝试使用 gmail 服务发送电子邮件,它在我的本地开发环境中运行完美,但我遇到了这个问题:

{code: 'EAUTH', response: '535-5.7.8 Username and Password not accepted. Lear…mail/?p=BadCredentials e19sm4851511qty.16 - gsmtp', responseCode: 535, command: 'AUTH PLAIN'}

我在谷歌上搜索了很多并查看了文档,但我发现的只是打开 Google 帐户中的“不太安全的应用程序访问”选项,我做到了,我尝试将其关闭,然后打开,我确保它在我的本地机器上工作,但不幸的是,当我将请求发送到 Vercel 端点中的 API 时它不工作。

我的 React 代码

const sendContactForm = (e) => {
    e.preventDefault()

    Axios.post(
      `${
        process.env.NODE_ENV === 'development'
          ? process.env.REACT_APP_API_LOCAL_URL
          : process.env.REACT_APP_API_URL
      }/contact`,
      {
        theName,
        email,
        subject,
        msg
      }
    )
      .then(({ data }) => {
        setSendStatus(data.sendStatus)
        setSendStatusMsg(data.sendStatusMsg)
        console.log(data)
      })
      .catch((err) => console.error(err))
  }

我的 NodeJS 代码是:

const nodemailer = require('nodemailer')
const smtpTransport = require('nodemailer-smtp-transport')

module.exports = (req, res) => {
  const { theName, email, msg, subject } = req.body
  // const { MAILER_EMAIL, MAILER_PASS } = process.env

  let transporter = nodemailer.createTransport(
    smtpTransport({
      service: 'gmail',
      port: 465,
      // secure: true,
      auth: {
        user: process.env.MAILER_EMAIL,
        pass: process.env.MAILER_PASS
      }
    })
  )

  const mailOptions = {
    from: email,
    to: process.env.MAILER_EMAIL,
    subject: subject,

    html: `<p>here is simply HTML code</p>`
  }

  transporter.sendMail(mailOptions, (err, info) => {
    if (err) {
      res.send({
        sendStatusMsg: 'Sorry! something went wrong!',
        errResponse: err,
        sendStatus: 0
      })
    } else {
      res.send({
        sendStatusMsg: 'Thanks for contacting us',
        infoResponse: info.response,
        sendStatus: 1
      })
    }
  })
}

非常感谢。

我发现 this question 有同样的问题。除了启用安全性较低的应用程序外,它还有其他建议,您可以查看它并查看是否有帮助

此外,有些人建议使用 XOAuth2 来验证