5

I'm trying to use nodemailer(npm package) in my node app to send email through the contact page. It is giving me this 535 Authentication Failed error while I can assure you that my email and password are absolutely correct.

var express = require('express');
var router = express.Router();
const nodemailer = require("nodemailer");
require('dotenv').config();

router.route('/')
    .post((req, res)=>{
        const emailData=req.body;
        let transporter = nodemailer.createTransport({
            host: "smtp.zoho.in",
            port: 465,
            secure: true, // true for 465, false for other ports
            auth: {
                user: process.env.EMAIL_ID, // generated ethereal user
                pass: process.env.EMAIL_PASS, // generated ethereal password
            },
            tls:{
                rejectUnauthorized: false
            }
        });
        
    let info = transporter.sendMail({
        from: process.env.EMAIL_ID, // sender address
        to: process.env.EMAIL_ID, // list of receivers
        subject: "Quengenesis: Contact Message", // Subject line
        text: `
            From: ${emailData.fName} ${emailData.lName}
            Email: ${emailData.email}
            Phone: ${emailData.phone}
            Message: ${emailData.message}`, // plain text body
        // html: "<b>Hello world?</b>", // html body
    });
    
    // console.log("Message sent: %s", info.messageId);
    // console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
    // verify connection configuration
    transporter.verify(function(err, success) {
        if (err) {
            res.send('There is a problem in the server, please try again later '+ err);
        } 
        else {
            res.send('Your message was sent successfully');
        }
    });
    
})

module.exports = router;

Nishant Kumar
  • 691
  • 1
  • 11
  • 31
  • Does this answer your question? [Zoho mail says 535 Authentication Failed in Node Js](https://stackoverflow.com/questions/54079576/zoho-mail-says-535-authentication-failed-in-node-js) – Chukwuemeka Maduekwe Apr 12 '22 at 17:28

5 Answers5

4

I had a domain-based email address like you@yourdomain.com and I was getting this error.

Solution for me was to use

host: 'smtppro.zoho.in'

in place of

host: "smtp.zoho.com"

This is documented in this article

Also, do create an app password from this link. Full help doc

Full createTransport

let transporter = nodemailer.createTransport({
   host: 'smtppro.zoho.in',
   secure: true,
   port: 465,
   auth: {
      user: YOU@YOURDOMAIN.COM,
      pass: ZOHO_APP_PASSWORD,
   },
});
user3884753
  • 255
  • 6
  • 16
1

I enabled two-factor authentication in my Zoho account and then I created a separate app password from here under the security tab then used this password for the nodemailer. It worked.

Nishant Kumar
  • 691
  • 1
  • 11
  • 31
1

I was also facing a 535 authentication fail issue using nodemailer. I solved this issue by changing password:

the previous password included special characters e.g. !@ the new password contains only alphabetic and Numeric characters e.g HwdO6ncdh4

Ahmad Javed
  • 48
  • 1
  • 1
  • 9
0

At the transporter options, if you use gmail at the auth, you need to add
auth: { user: 'youremail@gmail.com', pass: 'yourpassword' }
You must also know that you are the only one that can send email to other users.
When sending email at the transporter the from: process.env.EMAIL_ID can only be your email, not anyone else's.
If you use yahoo it will be a bit more tricky to send emails, because you need to add an application password in order to authenticate.

Denis Onu
  • 41
  • 1
  • 2
  • 1
    If you go through my code properly once again you will realize, I'm using a business email. It's neither Gmail nor Yahoo. Did you check the host that I've used in my code? I think you are not much aware of environment variables. – Nishant Kumar Jan 31 '21 at 20:39
  • I see. I read an article on medium and I saw that you need an app password for the zoho service. https://medium.com/@bluedesk09/sending-email-with-zoho-nodejs-nodemailer-62de7fffc8ac – Denis Onu Feb 01 '21 at 07:52
  • I'm not using two-factor authentication so I don't need to generate another password, however, I tried with a new password as well also got rid of the environment variable but the error remains as it is. – Nishant Kumar Feb 01 '21 at 10:21
  • You also need to know that you are the only one that can send emails. The sender address can be only your email address. – Denis Onu Feb 01 '21 at 11:51
  • yes, I know, I assure you the sender address is only my email address. Here process.env.EMAIL_ID has been assigned only my business email address created with Zoho. – Nishant Kumar Feb 01 '21 at 12:29
  • Interesting. Try deleting the `tls: {rejectUnauthorized: false }` and see if it works. Otherwise add `await transporter.sendMail` so it's asynchronous. – Denis Onu Feb 01 '21 at 13:33
  • that's for testing on localhost, nothing to do with authentication – Nishant Kumar Feb 01 '21 at 14:16
  • I enabled two-factor authentication and then created an app password as per your suggestion. Initially, I didn't have two-factor authentication. Anyway, it worked and I'm happy, thanks, mate :) – Nishant Kumar Feb 01 '21 at 15:35
  • Interesting. For me, i have two-factor auth OFF and when i pass it id and pw as text inside code it works, but with env var it does not work... any clue why..? – Leonard Aug 30 '21 at 04:40
0

Make sure the email id and password of the email domain you are using are correct. Error messages from nodemailer may be tricky and have you frustrated is values to these are not meticulously written