Username and Password not accepted when using nodemailer?

旧时模样 提交于 2019-11-28 09:53:10

I think that first you need to Allow less secure apps to access account setting in your google account - by default this settings is off and you simply turn it on.

Then I use the following script to send emails from a gmail account, also tested with yahoo and hotmail accounts.

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');
});

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

node send-email

You should see in the console - success, if the email was send successfully or the error message returned by 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!

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.

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.)

Your script will now look like this:

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

I hope this helps someone.

You are using gmail service so your mail must be in 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');
    } 
}); 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!