node.js 使用 nodemailer 时不接受用户名和密码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45478293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:24:07  来源:igfitidea点击:

Username and Password not accepted when using nodemailer?

node.jsnodemailer

提问by uma raja

This is my settingController:

这是我的设置控制器:

var sendSmtpMail = function (req,res) {
  var transport = nodemailer.createTransport({
  service:'gmail',
   auth: {
             user: "[email protected]",
             pass: "qwerr@wee"
        }
   });
   var mailOptions = {
        from: "[email protected]", 
        to:'[email protected]', 
        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');
      } 
  }); 

in postman I got the error like that:

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

Email could not sent due to error:

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

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

回答by codtex

I think that first you need to Allow less secure apps to access accountsetting in your google account - by default this settings is offand you simply turn it on. Also you need to make sure that 2 factor authentication for the account is disabled. You can check how to disable it here.

我认为首先您需要允许不太安全的应用程序访问您的 google 帐户中的帐户设置 - 默认情况下,此设置处于关闭状态,您只需将其打开即可。您还需要确保禁用帐户的 2 因素身份验证。您可以在此处查看如何禁用它。

Then I use the following script to send emails from a gmailaccount, also tested with yahooand hotmailaccounts.

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

const nodemailer = require('nodemailer');

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

let mailOptions = {
    from: '[email protected]',
    to: '[email protected]',
    subject: 'Test',
    text: 'Hello World!'
};

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

If you put the previous code in send-email.jsfor example, open terminal and write:

例如,如果您将前面的代码放在send-email.js中,请打开终端并编写:

node send-email

You should see in the console - success, if the email was send successfully or the error message returned by nodemailer

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

Don't forget to first do the setting - Allow less secure apps to access account.

不要忘记先进行设置 -允许不太安全的应用程序访问帐户

I hope this code will be useful for you. Good Luck!

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

回答by dcdc

If you have enabled 2-factor authentication on your Google account you can't use your regular password to access Gmail programmatically. You need to generate an app-specific password and use that in place of your actual password.

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

Steps:

脚步:

  • Log in to your Google account
  • Go to My Account > Sign-in & Security > App Passwords
  • (Sign in again to confirm it's you)
  • Scroll down to Select App (in the Password & sign-in method box) and choose Other (custom name)
  • Give this app password a name, e.g. "nodemailer"
  • Choose Generate
  • Copy the long generated password and paste it into your Node.js script instead of your actual Gmail password. (You don't need the spaces.)
  • 登录您的 Google 帐户
  • 转到我的帐户 > 登录和安全 > 应用密码
  • (再次登录以确认是您本人)
  • 向下滚动到选择应用程序(在密码和登录方法框中)并选择其他(自定义名称)
  • 为这个应用程序密码命名,例如“nodemailer”
  • 选择生成
  • 复制生成的长密码并将其粘贴到您的 Node.js 脚本中,而不是您实际的 Gmail 密码。(您不需要空格。)

Your script will now look like this:

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

var transporter = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: '[email protected]',
    pass: 'YOUR-GENERATED-APP-PASSWORD'
  }
});

I hope this helps someone.

我希望这可以帮助别人。

回答by GilbertS

Make sure you are using the right port: ie port: 587as of now.

确保您使用的是正确的端口:即port: 587现在。

Visit this link to allow Less secure App access: https://myaccount.google.com/lesssecureapps

访问此链接以允许不太安全的应用程序访问:https: //myaccount.google.com/lesssecureapps

Enable the recaptcha at this link: https://accounts.google.com/b/0/DisplayUnlockCaptcha

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

Wait for a few minutes and try to send an email.

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

Alternatively use sendgrid. It has a free version.

或者使用sendgrid。它有一个免费版本。

If you are not seeing the emails in your inbox, check the Spam folder.

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

回答by trungducng

You are using gmail service so your mail must be in gmail:

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

var transport = nodemailer.createTransport({
service:'gmail',
 auth: {
             user: "[email protected]",
             pass: "qwerr@wee"
        }
    });
var mailOptions = {
        from: "[email protected]", 
        to:'[email protected]', 
        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');
    } 
});