-1

I am new at node js. I want to make a contact form using nodemailer but i am getting an error message repeatedly and this is like username and pass are not accepted . I am trying to fix the problem but i cant . google less secure apps doesnot working..Here is my code and console 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 a2-20020a17090a70c200b001e2a36fb4aasm4621401pjm.43 - gsmtp
    at SMTPConnection._formatError (C:\Users\Asus\Documents\online education\node_modules\nodemailer\lib\smtp-connection\index.js:784:19)
    at SMTPConnection._actionAUTHComplete (C:\Users\Asus\Documents\online education\node_modules\nodemailer\lib\smtp-connection\index.js:1536:34)
    at SMTPConnection.<anonymous> (C:\Users\Asus\Documents\online education\node_modules\nodemailer\lib\smtp-connection\index.js:540:26)
    at SMTPConnection._processResponse (C:\Users\Asus\Documents\online education\node_modules\nodemailer\lib\smtp-connection\index.js:947:20)
    at SMTPConnection._onData (C:\Users\Asus\Documents\online education\node_modules\nodemailer\lib\smtp-connection\index.js:749:14)
    at TLSSocket.SMTPConnection._onSocketData (C:\Users\Asus\Documents\online education\node_modules\nodemailer\lib\smtp-connection\index.js:189:44)
    at TLSSocket.emit (events.js:400:28)
    at addChunk (internal/streams/readable.js:290:12)
    at readableAddChunk (internal/streams/readable.js:265:9)
    at TLSSocket.Readable.push (internal/streams/readable.js:204:10) {
  code: 'EAUTH',
  response: '535-5.7.8 Username and Password not accepted. Learn more at\n' +
    '535 5.7.8  https://support.google.com/mail/?p=BadCredentials a2-20020a17090a70c200b001e2a36fb4aasm4621401pjm.43 - gsmtp',  
  responseCode: 535,
  command: 'AUTH PLAIN'
}
const express= require('express');
const app=express();
const nodemailer = require('nodemailer');

const PORT=process.env.PORT || 5000;

app.use(express.static('public'));
app.use(express.json())

app.get('/',(req,res)=>{
    res.sendFile(__dirname + '/public/contact.html');
});

app.post('/',(req,res)=>{
    console.log(req.body);

    const transporter= nodemailer.createTransport({
        service:'gmail',
        auth: {
            user:'tr8622043',
            pass: 'trisha082215'
        },
    });
    const mailOptions={
        from: req.body.email,
        to:'tr8622043@gmail.com',
        subject:`Message from ${req.body.email} : ${req.body.subject} `,
        text: req.body.message
    }
    transporter.sendMail(mailOptions,(error,info)=>{
        if(error){
            console.log(error);
            res.send('error');
        }else{
            console.log('Email sent:'+ info.response);
            res.send('successful');
        }
    })
})

app.listen(PORT,()=>{
    console.log(`Server running on port ${PORT}`)
})
Trisha
  • 1
  • 1
  • Take a look at this article [username-and-password-not-accepted-when-using-nodemailer](https://stackoverflow.com/questions/45478293/username-and-password-not-accepted-when-using-nodemailer#:~:text=%20Steps%3A%20%201%20Log%20in%20to%20your,your%20Node.js%20script%20instead%20of%20your...%20More%20) – Morteza Jun 12 '22 at 07:03
  • Did you open access from gmail to nodejs apps? – yanir midler Jun 12 '22 at 07:33
  • how can i access ? – Trisha Jun 12 '22 at 08:55
  • https://stackoverflow.com/questions/72589948/why-does-nodemailer-throw-the-error-invalid-credentials/72598369#72598369 You can refer this. I just added detailed description – krupali makadiya Jun 13 '22 at 06:12

1 Answers1

0

Maybe you can try this

  1. Go to Google Account -> Security -> Signing in to Google - App Passwords
  2. Select Mail & Computer Next click generate (replace the current password in your code)

Make sure your 2-step verification is on first

Ps: Don't forget to restart your server

shakira
  • 9
  • 1