0

I tried to send e-mail using Nodemailer in Node.js but it is not working. I don't know why it is not working. If anyone knows, please help, to find the solution.

Getting this 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 9sm14519585pfh.160 - gsmtp

data.controller.js:

const nodemailer = require('nodemailer');

// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: 'mygmail@gmail.com',
        pass: 'mypass'
    }
});
 
let mailOptions = {
    from: "mygmail@example.com", // sender address
    subject: "Hello ✔", // Subject line
    text: "Hello This is an auto generated Email for testing  from node please ignore it", // plaintext body
    to: "togmail@gmail.com"
}
 
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) {
    if (error) {
        return console.log(error);
    }
    console.log('Message %s sent: %s', info.messageId, info.response);
});
Suresh Mangs
  • 705
  • 8
  • 19
Pappa S
  • 303
  • 1
  • 8
  • 21
  • duplicate: https://stackoverflow.com/questions/20362145/nodemailer-ssl-and-gmail-problems – Vahid Alimohamadi Jul 04 '20 at 18:04
  • @cybercoder: Above the link already i have tried but no use..If i use that concept i am getting one more error like events.js:287 throw er; // Unhandled 'error' event ^ TypeError: Cannot create property 'mailer' on string 'SMTP' – Pappa S Jul 04 '20 at 18:08
  • @Pappa S - You need to get SMTP server details and credential. Try sendgrid SMTP for free. You cannot trigger mails from your Gmail accounts. – Shiva Jul 04 '20 at 18:40
  • @shiva:: SMTP server is free or need to pay? – Pappa S Jul 05 '20 at 03:50
  • @PappaS It's free with limited functionality and mails. – Shiva Jul 24 '20 at 21:59

1 Answers1

0
const nodemailer = require('nodemailer');

const email = 'myemail@gmail.com';
const password = '**********';

You should set smtp host as smtp.gmail.com.
You can set smtp port number as 587 or 465.
587 is tls and 465 is ssl.

var transporter = nodemailer.createTransport({
     host: 'smtp.gmail.com',
     port: 587,
     secure: false,
     auth: {
         user: `${email}`,
         pass: `${password}`
     }
});

transporter.sendMail(mailOptions, function (error, info) {
if (error) {
    console.log(error);
} else {
    console.log('Email sent: ' + info.response);
    var mail = 'message';
    const from_email = 'sender@email.com';
    const to_email = 'receiver@email.com';
    var mailOptions = {
        from: email,
        to: to_email,
        subject: 'Your subject',
        text: mail
    };
}
});

I used above code, so that I sent mail.
Please try this.

HRM
  • 73
  • 1
  • 5