简体   繁体   English

nodemailer 登录无效:535 身份验证失败

[英]nodemailer Invalid login: 535 Authentication Failed

I'm trying to use nodemailer(npm package) in my node app to send email through the contact page.我正在尝试在我的节点应用程序中使用 nodemailer(npm 包)通过联系页面发送 email。 It is giving me this 535 Authentication Failed error while I can assure you that my email and password are absolutely correct.它给了我这个 535 Authentication Failed 错误,而我可以向你保证我的 email 和密码是绝对正确的。

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; module.exports = 路由器;

At the transporter options, if you use gmail at the auth, you need to add在传输器选项中,如果您在 auth 使用 gmail,则需要添加
auth: { user: 'youremail@gmail.com', pass: 'yourpassword' }
You must also know that you are the only one that can send email to other users.您还必须知道,您是唯一可以将 email 发送给其他用户的人。
When sending email at the transporter the from: process.env.EMAIL_ID can only be your email, not anyone else's.在传输器上发送 email 时, from: process.env.EMAIL_ID只能是您的 email,而不是其他任何人的。
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.如果您使用 yahoo,发送电子邮件会有点棘手,因为您需要添加应用程序密码才能进行身份验证。

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.我在我的 Zoho 帐户中启用了双重身份验证,然后我在安全选项卡下从此处创建了一个单独的应用程序密码,然后将此密码用于 nodemailer。 It worked.有效。

I had a domain-based email address like you@yourdomain.com and I was getting this error.我有一个基于域的 email 地址,例如 you@yourdomain.com,我收到了这个错误。

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完整的 createTransport

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM