错误:连接 ECONNREFUSED 127.0.0.1:587 – nodemailer 和 gmail

问题描述 投票:0回答:2

亲爱的堆栈溢出社区,

我正在处理一个项目,我尝试了很多东西,但我无法处理它。

我用 NextJS 创建了一个网站。该网站有一个联系表格。要将数据从联系表单发送给收件人,有一个 API 可以接受请求,并且通过

nodemailer
我正在尝试创建一个传输程序来使用 Gmail 发送电子邮件。

const nodemailer = require("nodemailer");
const { google } = require("googleapis");

export default async function (req, res) {
  const body = JSON.parse(req.body);

  const CLIENT_EMAIL = process.env.CLIENT_EMAIL;
  const CLIENT_ID = process.env.CLIENT_ID;
  const CLIENT_SECRET = process.env.CLIENT_SECRET;
  const REDIRECT_URI = process.env.REDIRECT_URI;
  const REFRESH_TOKEN = process.env.REFRESH_TOKEN;

  const OAuth2Client = new google.auth.OAuth2(
    CLIENT_ID,
    CLIENT_SECRET,
    REDIRECT_URI
  );

  OAuth2Client.setCredentials({ refresh_token: REFRESH_TOKEN });

  try {
    // Generate the accessToken on the fly
    const accessToken = await OAuth2Client.getAccessToken();

    // Account authentication config
    const authConfig = {
      type: "OAuth2",
      user: CLIENT_EMAIL,
      clientId: CLIENT_ID,
      clientSecret: CLIENT_SECRET,
      refreshToken: REFRESH_TOKEN,
      accessToken: accessToken,
    };

    // Create the email envelope (transport)
    const transport = nodemailer.createTransport(authConfig);

    // Create the email options and body
    // ('email': user's email and 'name': is the e-book the user wants to receive)
    const mailOptions = {
      from: `${CLIENT_EMAIL}`,
      to: body.email,
      subject: `${body.name} – ${body.reason}`,
      text: `${body.message}`,
    };

    const result = await transport.sendMail(mailOptions);
    return result;
  } catch (error) {
    console.log(error);
  }

  res.status(200).json({
    message: "Email has been sent",
  });
}

问题是,当用户发送电子邮件时,我收到以下错误消息:

Error: connect ECONNREFUSED 127.0.0.1:587
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16) {
  errno: -61,
  code: 'ESOCKET',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 587,
  command: 'CONN'
}

我现在尝试了一些方法,但目前我确实没有取得任何进展。如果有人能帮助我,我会非常高兴。

提前非常感谢。

node.js google-api gmail gmail-api nodemailer
2个回答
1
投票

只需将 http://localhost:8000 改为 http://127.0.0.1:8000。有用。 Windows 没有配置来解决这个问题。 IP 地址应该采用数字约定。

http://127.0.0.1:8000

以上地址是任何电脑的默认地址


0
投票

尝试记录环境变量以确定。另外,您可能想正确加载环境变量

确保已安装 dotenv:

npm i dotenv

然后将其加载到该模块中

require('dotenv').config();

或者 ES6

import "dotenv/config";

更改后您的代码可能看起来像这样:

require('dotenv').config();
const nodemailer = require("nodemailer");
const { google } = require("googleapis");

export default async function (req, res) {
  const body = JSON.parse(req.body);

  const CLIENT_EMAIL = process.env.CLIENT_EMAIL;
  const CLIENT_ID = process.env.CLIENT_ID;
  const CLIENT_SECRET = process.env.CLIENT_SECRET;
  const REDIRECT_URI = process.env.REDIRECT_URI;
  const REFRESH_TOKEN = process.env.REFRESH_TOKEN;

  const OAuth2Client = new google.auth.OAuth2(
    CLIENT_ID,
    CLIENT_SECRET,
    REDIRECT_URI
  );

  OAuth2Client.setCredentials({ refresh_token: REFRESH_TOKEN });

  try {
    // Generate the accessToken on the fly
    const accessToken = await OAuth2Client.getAccessToken();

    // Account authentication config
    const authConfig = {
      type: "OAuth2",
      user: CLIENT_EMAIL,
      clientId: CLIENT_ID,
      clientSecret: CLIENT_SECRET,
      refreshToken: REFRESH_TOKEN,
      accessToken: accessToken,
    };

    // Create the email envelope (transport)
    const transport = nodemailer.createTransport(authConfig);

    // Create the email options and body
    // ('email': user's email and 'name': is the e-book the user wants to receive)
    const mailOptions = {
      from: `${CLIENT_EMAIL}`,
      to: body.email,
      subject: `${body.name} – ${body.reason}`,
      text: `${body.message}`,
    };

    const result = await transport.sendMail(mailOptions);
    return result;
  } catch (error) {
    console.log(error);
  }

  res.status(200).json({
    message: "Email has been sent",
  });
}
© www.soinside.com 2019 - 2024. All rights reserved.