【问题标题】:Username and Password not accepted when using nodemailer?使用 nodemailer 时不接受用户名和密码?
【发布时间】:2017-08-03 08:03:33
【问题描述】:

这是我的设置控制器:

var sendSmtpMail = function (req,res) {
  var transport = nodemailer.createTransport({
  service:'gmail',
   auth: {
             user: "asdfqweerrccb@limitlesscircle.com",
             pass: "qwerr@wee"
        }
   });
   var mailOptions = {
        from: "transactions@limitlesscircle.com", 
        to:'umaraja1124@gmail.com', 
        subject: req.body.subject+"nodejs working ?", 
        text: "Hello world ?",  
    }
    transport.sendMail(mailOptions, function(error, response){
      if(error){
         res.send("Email could not sent due to error: "+error);
         console.log('Error');
       }else{
         res.send("Email has been sent successfully");
         console.log('mail sent');
      } 
  }); 

在邮递员中我得到了这样的错误:

由于错误无法发送电子邮件:

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 g7sm64435626pfj.29 - gsmtp

【问题讨论】:

  • 您能否尝试为 auth 和 from key 使用相同的帐户

标签: node.js nodemailer


【解决方案1】:

我认为首先您需要在您的 Google 帐户中设置Allow less secure apps to access account - 默认情况下此设置为关闭,您只需将其打开。此外,您需要确保禁用该帐户的 2 因素身份验证。您可以查看如何禁用它here

然后我使用以下脚本从 gmail 帐户发送电子邮件,该帐户还使用 yahoohotmail 帐户进行了测试。

const nodemailer = require('nodemailer');

let transporter = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    port: 587,
    secure: false,
    requireTLS: true,
    auth: {
        user: 'your.gmail.account@gmail.com',
        pass: 'your.password'
    }
});

let mailOptions = {
    from: 'your.gmail.account@gmail.com',
    to: 'receivers.email@domain.com',
    subject: 'Test',
    text: 'Hello World!'
};

transporter.sendMail(mailOptions, (error, info) => {
    if (error) {
        return console.log(error.message);
    }
    console.log('success');
});

如果你把前面的代码放在send-email.js中,比如打开终端,写:

node send-email

您应该在控制台中看到 - success,如果电子邮件发送成功或nodemailer返回的错误消息

别忘了先做设置 - Allow less secure apps to access account

我希望这段代码对你有用。祝你好运!

【讨论】:

  • 这个在 gmail 中对我有用。但是我收到的公司域从来没有收到过这样的问候
  • 我不太明白你想说什么,抱歉。您是否想说这些电子邮件是由其他 gmail 帐户而不是您公司的电子邮件帐户接收的?
  • 仅供参考,如果您启用了 2F 身份验证,则禁用“不太安全的应用程序”功能。您需要生成应用密码。为此,请按照以下步骤操作:support.google.com/accounts/answer/185833
  • 对 gmail 的一个重要警告是,您需要关闭 2 因素身份验证以及允许的安全性较低的应用程序。除非您提供 auth.xoauth2 访问令牌。否则会失败。
  • 感谢大家指出这一点。我在解释中添加了这个额外的步骤。
【解决方案2】:

如果您在 Google 帐户上启用了 2 因素身份验证,则无法使用常规密码以编程方式访问 Gmail。您需要生成一个应用专用密码并使用该密码代替您的实际密码。

步骤:

  • 登录您的 Google 帐户
  • 转到我的帐户 > 登录和安全 > 应用密码
  • (再次登录以确认是您)
  • 向下滚动到选择应用程序(在密码和登录方法框中) 并选择其他(自定义名称)
  • 为此应用程序密码命名,例如“节点邮件”
  • 选择生成
  • 复制生成的长密码并将其粘贴到您的 Node.js 脚本中,而不是您的实际 Gmail 密码。 (您不需要空格。)

您的脚本现在将如下所示:

var transporter = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: 'YOUR-GMAIL-USERNAME@gmail.com',
    pass: 'YOUR-GENERATED-APP-PASSWORD'
  }
});

我希望这对某人有所帮助。

【讨论】:

  • 留下一个网站供您参考:support.google.com/accounts/answer/185833?hl=en
  • 除此之外,我还必须启用验证码(下面的链接)https://accounts.google.com/DisplayUnlockCaptcha 然后它对我有用。
  • 非常感谢您的回答。这真的很有帮助,因为启用“允许不太安全的应用访问帐户”是不够的。
【解决方案3】:

https://myaccount.google.com/lesssecureapps 如果它处于关闭状态,请将其打开以启用不太安全的应用程序。 (我有同样的问题打开它来解决我的问题)

【讨论】:

    【解决方案4】:

    确保您使用的是正确的端口:截至目前,port: 587

    访问此链接以允许安全性较低的应用访问:https://myaccount.google.com/lesssecureapps

    在此链接启用验证码:https://accounts.google.com/b/0/DisplayUnlockCaptcha

    等待几分钟,然后尝试发送电子邮件。

    也可以使用sendgrid。它有一个免费版本。

    如果您没有在收件箱中看到电子邮件,请检查垃圾邮件文件夹。

    【讨论】:

      【解决方案5】:

      我遇到了同样的问题,我解决了如下:

      1. 转到https://admin.google.com 并使用您用于设置企业帐户和创建用户的主帐户登录。请记住使用主电子邮件 ID。

      1. 单击安全图标,您将被带到此页面,您将看到不太安全的应用程序部分,单击它。

      2. 现在你会看到这个,允许用户或在这里授予权限。

      3. 你还没有完成,不要去下面的链接: https://myaccount.google.com/u/1/lesssecureapps

      现在您将看到开关。启用它并尝试它肯定会起作用。

      和平:)

      【讨论】:

        【解决方案6】:

        有一些帐户设置是您使用 SMTP 通过它发送所必需的。

        如果您在帐户上启用了两步验证,则需要在设备设置中使用应用程序专用密码(在 Gmail 帐户中创建):使用应用程序专用密码登录

        如果没有,请尝试以下操作:使用网络浏览器https://mail.google.com 登录 Gmail 帐户,然后转到设置 > 帐户和导入 > 其他 Google 帐户设置。在安全性下,向下滚动并启用对安全性较低的应用程序的访问。启用 SMTP、POP 或 IMAP 访问需要此设置。

        如果仍有问题,请尝试清除验证码:访问 https://accounts.google.com/DisplayUnlockCaptcha 并使用 Gmail 用户名和密码登录。如有必要(通常​​不需要),输入扭曲图片中的字母,然后按继续。这将允许设备在十分钟内注册为已批准的连接。请注意,您必须使用您尝试添加到设备的帐户 - 如果浏览器已登录另一个帐户,您必须先退出。此外,您必须在按继续后十分钟内触发设备建立连接。

        Google 支持团队的解释

        【讨论】:

          【解决方案7】:

          您必须允许 Less-Secure-Apps 访问您的 google 帐户中的帐户设置 - 默认情况下,此设置处于关闭状态,您只需将其打开即可。 Image example

          【讨论】:

            【解决方案8】:

            您使用的是 gmail 服务,因此您的邮件必须在 gmail 中:

            var transport = nodemailer.createTransport({
            service:'gmail',
             auth: {
                         user: "asdfqweerrccb@gmail.com",
                         pass: "qwerr@wee"
                    }
                });
            var mailOptions = {
                    from: "transactions@gmail.com", 
                    to:'umaraja1124@gmail.com', 
                    subject: req.body.subject+"nodejs working ?", 
                    text: "Hello world ?",  
                }
            transport.sendMail(mailOptions, function(error, response){
                if(error){
                     res.send("Email could not sent due to error: "+error);
                     console.log('Error');
                }else{
                     res.send("Email has been sent successfully");
                     console.log('mail sent');
                } 
            }); 
            

            【讨论】:

            • 由于错误无法发送电子邮件:错误:无效登录:534-5.7.14 accounts.google.com/signin/continue?
            • @trungducng 请尝试在邮件选项中从密钥和身份验证中使用相同的电子邮件地址
            • 我得到了结果
            • 如何向多个用户发送邮件
            猜你喜欢
            • 2019-06-10
            • 2022-08-12
            • 1970-01-01
            • 1970-01-01
            • 2022-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-12-20
            相关资源
            最近更新 更多