gmail 的 Nodemailer 授权错误

Nodemailer authorization error with gmail

我尝试将 nodemailer 连接到 gmail。更具体地说,我想将联系表与后端连接起来。尽管多次尝试连接这两种服务,但我不断收到错误消息。出现一条消息,指出登录名和密码不匹配或未被接受。我登录到浏览器,一切正常。我仔细按照将 nodemailer 连接到 gmail 的说明进行操作。它是关于允许 gmail 连接到安全性较低的应用程序。我已经关闭了 gmail 中的双因素身份验证。不幸的是,它仍然没有帮助。我的代码如下。

ContactForm.js

import React, { useState } from "react";

const ContactForm = () => {
  const [status, setStatus] = useState("Submit");
  const handleSubmit = async (e) => {
    e.preventDefault();
    setStatus("Sending...");
    const { name, email, message } = e.target.elements;
    let details = {
      name: name.value,
      email: email.value,
      message: message.value,
    };
    let response = await fetch("http://localhost:5000/contact", {
      method: "POST",
      headers: {
        "Content-Type": "application/json;charset=utf-8",
      },
      body: JSON.stringify(details),
    });
    setStatus("Submit");
    let result = await response.json();
    alert(result.status);
  };
  return (
    <form onSubmit={handleSubmit}>
      <div>
        <label htmlFor="name">Name:</label>
        <input type="text" id="name" required />
      </div>
      <div>
        <label htmlFor="email">Email:</label>
        <input type="email" id="email" required />
      </div>
      <div>
        <label htmlFor="message">Message:</label>
        <textarea id="message" required />
      </div>
      <button type="submit">{status}</button>
    </form>
  );
};

export default ContactForm;

Index.js

const express = require("express");
const router = express.Router();
const cors = require("cors");
const nodemailer = require("nodemailer");

const app = express();
app.use(cors());
app.use(express.json());
app.use("/", router);
app.listen(5000, () => console.log("Server Running"));

const contactEmail = nodemailer.createTransport({
  host: 'smtp.gmail.com',
  port: 587,
  secure: false,
  auth: {
    user: "*********@gmail.com",
    pass: "**********",
  }
});

contactEmail.verify((error) => {
  if (error) {
    console.log(error);
  } else {


    console.log("Ready to Send");
  }
});

router.post("/contact", (req, res) => {
  const name = req.body.name;
  const email = req.body.email;
  const message = req.body.message; 
  const mail = {
    from: name,
    to: "",
    subject: "Contact Form Submission",
    html: `<p>Name: ${name}</p>
           <p>Email: ${email}</p>
           <p>Message: ${message}</p>`,
  };
  contactEmail.sendMail(mail, (error) => {
    if (error) {
      res.json({ status: "ERROR" });
    } else {
      res.json({ status: "Message Sent" });
    }
  });
});

运行 服务器之后 Node.js

Server Running
Error: Invalid login: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8  https://support.google.com/mail/?p=BadCredentials w4sm11053143qkd.94 - gsmtp
    at SMTPConnection._formatError (D:\my-website\backend\node_modules\nodemailer\lib\smtp-connection\index.js:774:19)
    at SMTPConnection._actionAUTHComplete (D:\my-website\backend\node_modules\nodemailer\lib\smtp-connection\index.js:1513:34)
    at SMTPConnection.<anonymous> (D:\my-website\backend\node_modules\nodemailer\lib\smtp-connection\index.js:540:26)  
    at SMTPConnection._processResponse (D:\my-website\backend\node_modules\nodemailer\lib\smtp-connection\index.js:932:20)
    at SMTPConnection._onData (D:\my-website\backend\node_modules\nodemailer\lib\smtp-connection\index.js:739:14)      
    at TLSSocket.SMTPConnection._onSocketData (D:\my-website\backend\node_modules\nodemailer\lib\smtp-connection\index.js:189:44)
    at TLSSocket.emit (events.js:315:20)
    at addChunk (internal/streams/readable.js:309:12)
    at readableAddChunk (internal/streams/readable.js:284:9)
    at TLSSocket.Readable.push (internal/streams/readable.js:223:10) {
  code: 'EAUTH',
  response: '535-5.7.8 Username and Password not accepted. Learn more at\n' +
    '535 5.7.8  https://support.google.com/mail/?p=BadCredentials w4sm11053143qkd.94 - gsmtp',
  responseCode: 535,
  command: 'AUTH PLAIN'
}

关闭双因素授权后,您还需要关闭 google 帐户中的设置以允许安全性较低的应用。您可以从 https://myaccount.google.com/lesssecureapps

开始

然后,它会给你一个随机生成的密码,用于 Nodemailer。您的实际密码无效。