Sending e-mail with Nodejs and nodemailer

Asked

Viewed 194 times

-2

I understand that there are some posts about how to send email using Nodejs and nodemailer, but after many attempts I still receive the fault Answer: '535-5.7.8 Username and Password not accepted when trying to send email.

I checked several times login and password, configured so that Google allows less secure applications.

inserir a descrição da imagem aqui

This glitch could be some line of code I haven’t found yet.

--> email js.

module.exports = {
    host: 'smtp.gmail.com',
    port: 587,
    user: '[email protected]',
    pass: 'minha senha',
};

const SMTP_CONFIG = require('../config/email');

const transporter = nodemailer.createTransport({
                        host: SMTP_CONFIG.host,
                        port: SMTP_CONFIG.port,
                        secure: false,
                        auth: {
                            user: SMTP_CONFIG.user,
                            pass: SMTP_CONFIG.pass
                        },
                        tls: {
                            rejectUnauthorized: false,
                        }
                    });

                    var welcome = {
                        from: SMTP_CONFIG.user,
                        to: '[email protected]',
                        subject: 'Teste de e-mail',
                        html: '<h1>Teste envio de e-mail</h1>'
                    };

                    transporter.sendMail(welcome, function (err, info) {
                        if (err) {
                            console.log(err);
                        }
                        else {
                            console.log('Email enviado: ' + info.response);
                        }
                    });

2 answers

0

The problem you are presenting may have 2 causes:

  • The constant SMTP_CONFIG may be malfunctioning.
  • Your google account has 2-Factor Auth enabled.

I tested your code with my credentials (replacing the previously mentioned variable with the global constant) and it works. So I believe it is one of the problems mentioned above.

  • Thanks Eduardo.. I checked the 2-Factor Auth and it was enabled.. You can classify my question as positive.. Many should have this kind of doubt / problem.

  • Now this. Just a repair, did not select this answer as correct and I think I helped him. About your question, I found the same clear. Continuation of good work!

0


const transporter = nodemailer.createTransport({
          service: 'gmail',
          auth: {
            user: '[email protected]',
            pass: 'senha',
            port: 587,
            secure: true,
          }
        });
    
    const mailOptions = {
          from: 'Meu nome <[email protected]>',
          to: 'destinatario <[email protected]>'
          subject: 'assunto',
          html: '<h5>Obrigada por visitar</h5>'
        };
    
    return transporter.sendMail(mailOptions, (erro, info) => {
          if (erro) {
            console.log('Erro ao enviar email');
            return reject(erro);
          }
          return resolve({ success: true });
        });
  • Thanks Paulo, with its configuration and the verification I did in the 2-Factor Auth, it worked. Can you evaluate my question as positva? I do not understand why the question was not clear or had no research effort..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.