【问题标题】:Nodemailer OAUTH Gmail connection (Error: connect ETIMEDOUT 74.125.140.108:465)Nodemailer OAUTH Gmail 连接(错误:连接 ETIMEDOUT 74.125.140.108:465)
【发布时间】:2019-12-24 03:07:21
【问题描述】:

我正在尝试使用 OAUTH 2.0 使用 Nodemailer 使用 Gmail API 发送电子邮件

我启用了 Gmail API,并且我有 clientId、clientSecret、refreshToken 和 accessToken(refreshtoken 和 accesstoken 取自 developer.google.com/oauthplayground),因为这些都是 OAUTH 工作所必需的。

我已经遵循了每一个详细的表格 (https://nodemailer.com/smtp/oauth2/#examples) 但我收到以下错误

{ Error: connect ETIMEDOUT 74.125.140.108:465
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1097:14)
  errno: 'ETIMEDOUT',
  code: 'ESOCKET',
  syscall: 'connect',
  address: '74.125.140.108',
  port: 465,
  command: 'CONN' }

但是当我使用普通用户名和密码时有一个问题,没有连接超时错误,因为我已经启用了不太安全的应用程序访问表单 (https://myaccount.google.com/lesssecureapps),因此使用普通用户名发送电子邮件没有错误和密码,但这不是一个好方法,所以 OAUTH 是解决方案

这是我的代码供您检查(它是一个快速应用程序)在 http://localhost:3000 上运行的开发中运行

var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
service: 'gmail',
auth: {
  type: 'OAuth2',
  user: 'mygmailaddress@gmail.com',
  clientId: 'clientID',
  clientSecret: 'clientSecret',
  refreshToken: 'refresh token obtained from https://developers.google.com/oauthplayground',
  accessToken: 'access token also obtained from https://developers.google.com/oauthplayground'
}
});
var mailOptions = {
    from: 'me <myemailaddress@gmail.com>',
    to: '',
    subject: 'Subject ',
    text: 'Some thing',
  };

  transporter.sendMail(mailOptions, (error, response) => {
    if(error){
        console.log(error);   // Always giving Error: connect ETIMEDOUT 74.125.140.108:465
    } else {
        console.log(response);
    }
});

它甚至不会尝试登录,因为登录失败时会出现凭据错误(使用普通用户名和密码检查)

*注意请不要将此问题标记为重复,因为我检查了几乎所有其他问题和答案,但没有任何效果 有人说这是因为防火墙设置我也检查过但没有用 那么问题是什么以及如何解决它

【问题讨论】:

    标签: oauth-2.0 smtp gmail-api nodemailer smtp-auth


    【解决方案1】:

    查看您的问题后,我假设在生成 OAuth 凭据时,例如(clienId 和 Clientsecret)您没有在连接屏幕上的范围内添加 https://mail.google.com/,因为您需要验证表单 google 才能为凭据添加这些范围

    您必须在 (https://developers.google.com/oauthplayground) 上将此添加到您的范围,但我确定您在生成凭据之前也已在同意屏幕上将此添加到您的范围中

    OAuth consent screen

    因为您处于开发模式并在本地主机上运行您的应用程序,所以您没有私有域和隐私政策

    在生成 clientId 和 Cliensecret 之前将 https://mail.google.com 添加到您的作用域将解决您的问题

    【讨论】:

      【解决方案2】:

      您的 gmail 端口号错误。将此格式用于 gmail

      const nodeMailer = require('nodemailer');
      
      let transporter = nodeMailer.createTransport({
          host: "smtp.gmail.com",
          port: 587,
          secure: false, // true for 587, false for other ports
          requireTLS: true,
          auth: {
              user: your email, 
              pass: your password, 
          },
      });
      
      let mailOptions = {
          from: 'from@gmail.com',
          to: 'to@gmail.com',
          subject: 'Sending Email using Node.js',
          text: 'That was easy!'
      };
      
      transporter.sendMail(mailOptions, function(error, info){
          if (error) {
             console.log(error);
          } else {
              console.log('Email sent: ' + info.response);
          }
      });
      

      【讨论】:

        猜你喜欢
        • 2016-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-13
        • 2020-08-09
        • 1970-01-01
        • 2021-12-06
        • 1970-01-01
        相关资源
        最近更新 更多