我收到错误“ 错误:无效登录:535-5.7.8 用户名和密码不被接受。”当我尝试使用 Node JS 中的模块 nodemailer 发送邮件时

Posted

技术标签:

【中文标题】我收到错误“ 错误:无效登录:535-5.7.8 用户名和密码不被接受。”当我尝试使用 Node JS 中的模块 nodemailer 发送邮件时【英文标题】:I get a error " Error: Invalid login: 535-5.7.8 Username and Password not accepted." when I try send a mail with the module nodemailer from Node JS我收到错误“ 错误:无效登录:535-5.7.8 用户名和密码不被接受。”当我尝试使用 Node JS 中的模块 nodemailer 发送邮件时 【发布时间】:2018-12-15 11:58:31 【问题描述】:

我是 Node js 的新手,我尝试向其他电子邮件发送类似示例的消息,当我从 linux 在终端中运行文件时,我收到此错误:

root@me:/home/memee/Desktop/test/NodeJS/mail# nano example.js
root@me:/home/memee/Desktop/test/NodeJS/mail# node example.js
 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 77-v6sm41437784pga.40 - gsmtp
    at SMTPConnection._formatError (/home/memee/Desktop/test/NodeJS/mail/node_modules/nodemailer/lib/smtp-connection/index.js:606:19)
    at SMTPConnection._actionAUTHComplete (/home/memee/Desktop/test/NodeJS/mail/node_modules/nodemailer/lib/smtp-connection/index.js:1335:34)
    at SMTPConnection._responseActions.push.str (/home/memee/Desktop/test/NodeJS/mail/node_modules/nodemailer/lib/smtp-connection/index.js:366:26)
    at SMTPConnection._processResponse (/home/memee/Desktop/test/NodeJS/mail/node_modules/nodemailer/lib/smtp-connection/index.js:762:20)
    at SMTPConnection._onData (/home/memee/Desktop/test/NodeJS/mail/node_modules/nodemailer/lib/smtp-connection/index.js:558:14)
    at TLSSocket._socket.on.chunk (/home/memee/Desktop/test/NodeJS/mail/node_modules/nodemailer/lib/smtp-connection/index.js:510:47)
    at emitOne (events.js:116:13)
    at TLSSocket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
  code: 'EAUTH',
  response: '535-5.7.8 Username and Password not accepted. Learn more at\n535 5.7.8  https://support.google.com/mail/?p=BadCredentials 77-v6sm41437784pga.40 - gsmtp',
  responseCode: 535,
  command: 'AUTH PLAIN' 

我知道我的错误是登录,但我的密码和电子邮件是正确的。

我的代码:

var nodemailer = require('nodemailer');

var transporter = nodemailer.createTransport(
  service: 'gmail',
  auth: 
    user: 'example@gmail.com',
    pass: 'here I put my password'
  
);

var 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);
  
);

我的 node 和 npm 版本:

root@me:~# node --version
v8.11.3
root@me:~# npm --v
6.1.0
root@me:~# 

我用过这个教程:tutorial

【问题讨论】:

【参考方案1】:

您的邮箱和密码不匹配,查看错误信息:response: '535-5.7.8 Username and Password not accepted.

查看这部分代码:

var transporter = nodemailer.createTransport(
  service: 'gmail',
  auth: 
    user: 'example@gmail.com', // here use your real email
    pass: 'here I put my password' // put your password correctly (not in this question please)
  
);

如果您使用您的用户名和密码登录,您将能够发送电子邮件。

我使用相同的代码发送这封电子邮件,但使用了我的凭据:

【讨论】:

【参考方案2】:

我最近遇到了这个问题,它与帐户上的 Less secure apps 设置有关。

选择“允许用户管理他们对不太安全的应用程序的访问”修复了它。

【讨论】:

使用当前的 google 安全性,在此修复后请立即查看 ***.com/a/51981381/6821441,因为您必须禁用验证码【参考方案3】:

在https://www.google.com/settings/security/lesssecureapps开启允许不太安全的应用程序

【讨论】:

【参考方案4】:

我之前也遇到过同样的问题,但是在 从用户名中删除 '@gmail.com' 之后对我来说效果很好。 错误 '535-5.7.8 Username and Password not accepted' 仅在以下提到的情况下发生:

    凭据错误。 在用户名中添加@gmail.com,避免使用! 启用双因素身份验证。如果是,则禁用它。 https://myaccount.google.com/lesssecureapps 如果它处于关闭状态,请将其打开以启用不太安全的应用程序。

【讨论】:

【参考方案5】:

这里是完整的解决方案。干杯!!!

使用默认 SMTP 传输创建可重用的传输器对象

 transporter = nodemailer.createTransport(
  host: 'smtp.gmail.com',
  port: 465,
  secure: true, 
  auth: 
      user: 'yourEmail', //Remove '@gmail.com' from your username.
      pass: 'yourPassword' 
   
 );

在https://www.google.com/settings/security/lesssecureapps开启允许不太安全的应用程序

允许不太安全的应用访问:https://myaccount.google.com/lesssecureapps

完成!!

如果有人遇到以下问题

(firebase functions) Error: Forbidden Your client does not have permission to get URL /

解决办法是:

删除您的云函数 更新 Firebase CLI npm install -g firebase-tools 重新部署您的函数

【讨论】:

【参考方案6】:

Step1 但是,您需要允许来自您的 google 帐户的安全性较低的应用程序发送电子邮件。 Through this link 。允许来自您的 Google 帐户的安全性较低的应用

阅读以下说明Enable imap

在 IMAP 访问部分启用 IMAP 的短 url GO TO URL

const nodemailer = require("nodemailer");
let transporter = nodemailer.createTransport(
host: "smtp.gmail.com",
port: 587,
secure: false, // true for 465, false for other ports
type:"oauth2",
auth: 
  user: "myusernmae@gmail.com", // generated ethereal user
  pass: "Mypassword", // generated ethereal password
);

transporter.verify(async function(error, success) 
if (error) 
  console.log(error);
 else 
  let info = await transporter.sendMail(
      from: '"Fred Foo ?" <foo@example.com>', // sender address
      to: "manoj@gmail.com, baz@example.com", // list of receivers
      subject: "Hello ✔", // Subject line
      text: "Hello world?", // plain text body
      html: "<b>Hello world?</b>", // html body
  );
  console.log(info);
  console.log("Server is ready to take our messages");
);

工作示例

【讨论】:

【参考方案7】:

我试过这个并为我工作:

#1 创建谷歌应用密码https://support.google.com/accounts/answer/185833

#2 用途:

host: "smtp.gmail.com",
port: 587,
auth: 
  user: "Your username here e.g:abc@gmail.com",
  pass: "Your password here"

【讨论】:

为我工作,谢谢。

以上是关于我收到错误“ 错误:无效登录:535-5.7.8 用户名和密码不被接受。”当我尝试使用 Node JS 中的模块 nodemailer 发送邮件时的主要内容,如果未能解决你的问题,请参考以下文章

让 nodemailer 使用不同的 gmail 帐户发送电子邮件

我收到错误错误类型参数一元减号和预期';'在 ':' 标记之前

我收到错误:致命错误:索引超出范围

我收到以下错误:[GraphQL 错误]:消息:任务

我收到此错误 NSURLErrorDomain 错误 -999

为啥我收到的 jwt 格式错误?