TypeError - 无效登录

我写了一些代码来在我的 nodejs 应用程序中实现 nodemailer 来发送邮件。


我写了这段代码:


var express = require("express");

var router = express.Router();

var nodemailer = require("nodemailer");


/* GET contact page. */

router.get("/", function (req, res, next) {

  res.render("contact", { title: "Contact" });

});


router.post("/send", function (req, res, next) {

  var transporter = nodemailer.createTransport({

    service: "Gmail",

    auth: {

      user: "MyEmailHere",

      pass: "password",

    },

    tls: {

      rejectUnauthorized: false

    }

  });


  var mailOptions = {

    from: "MyEmailHere",

    to: "myOtherEmailId",

    subject: "Website Submission",

    text:

      "You have a new submission with the following details...Name: " +

      req.body.name +

      " Email: " +

      req.body.email +

      " Message: " +

      req.body.message,

    html:

      "<p> You got a new submission with the following details...</p><ul></ul><li>Name: " +

      req.body.name +

      "</li><li>Email: " +

      req.body.email +

      "</li><li>Message: " +

      req.body.message +

      "</li></ul>",

  };


  transporter.sendMail(mailOptions, function (error, info) {

    if (error) {

      console.log(error);

      res.redirect("/");

    } else {

      console.log("Message Sent: " + info.response);

    }

  });

});


module.exports = router;

但我收到一条错误消息:“不接受用户名和密码。” 我应该使用我的真实密码吗?除了 Gmail 之外,nodemailer 是否还支持 Yahoomail?顺便说一下,我仍在学习并处于开发模式。一点帮助?


SMILET
浏览 91回答 2
2回答

阿晨1998

当我正确输入我自己的信息并打开“不太安全的应用程序”时,它完美地工作,请尝试这种方式注意(关键):确保“不太安全的应用程序”选项已打开 (如果您不知道如何打开,可以访问此处)注意:确保关闭两步验证var express = require("express");var router = express.Router();var nodemailer = require("nodemailer");const myGmailAccount = {&nbsp; mail: 'your_gmail_address', // MAIL&nbsp; passw: 'your_gmail_password', // PASSW};/* GET contact page. */router.get("/", function (req, res, next) {&nbsp; res.render("contact", { title: "Contact" });});router.post("/send", function (req, res, next) {&nbsp; var transporter = nodemailer.createTransport({&nbsp; &nbsp; service: "Gmail",&nbsp; &nbsp; auth: {&nbsp; &nbsp; &nbsp; user: myGmailAccount.mail,&nbsp; &nbsp; &nbsp; pass: myGmailAccount.passw,&nbsp; &nbsp; },&nbsp; &nbsp; tls: {&nbsp; &nbsp; &nbsp; rejectUnauthorized: false&nbsp; &nbsp; }&nbsp; });&nbsp; var mailOptions = {&nbsp; &nbsp; from: myGmailAccount.mail,&nbsp; &nbsp; to: 'emintayfur@icloud.com', // recipient mail&nbsp; &nbsp; subject: "Website Submission",&nbsp; &nbsp; text:&nbsp; &nbsp; &nbsp; "You have a new submission with the following details...Name: " +&nbsp; &nbsp; &nbsp; req.body.name +&nbsp; &nbsp; &nbsp; " Email: " +&nbsp; &nbsp; &nbsp; req.body.email +&nbsp; &nbsp; &nbsp; " Message: " +&nbsp; &nbsp; &nbsp; req.body.message,&nbsp; &nbsp; html:&nbsp; &nbsp; &nbsp; "<p> You got a new submission with the following details...</p><ul></ul><li>Name: " +&nbsp; &nbsp; &nbsp; req.body.name +&nbsp; &nbsp; &nbsp; "</li><li>Email: " +&nbsp; &nbsp; &nbsp; req.body.email +&nbsp; &nbsp; &nbsp; "</li><li>Message: " +&nbsp; &nbsp; &nbsp; req.body.message +&nbsp; &nbsp; &nbsp; "</li></ul>",&nbsp; };&nbsp; transporter.sendMail(mailOptions, function (error, info) {&nbsp; &nbsp; if (error) {&nbsp; &nbsp; &nbsp; console.log(error);&nbsp; &nbsp; &nbsp; res.redirect("/");&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; console.log("Message Sent: " + info.response);&nbsp; &nbsp; }&nbsp; });});

哆啦的时光机

是的,您需要输入您的真实信息,我建议您阅读此文本。尽管 Gmail 是开始发送电子邮件的最快方式,但除非您使用 OAuth2 身份验证,否则它绝不是一个更好的解决方案。Gmail 希望用户是真正的用户而不是机器人,因此它会针对每次登录尝试运行大量试探法,并阻止任何看起来可疑的内容,以保护用户免受帐户劫持企图。例如,如果您的服务器位于另一个地理位置,您可能会遇到麻烦——在您的开发机器上一切正常,但消息在生产环境中被阻止。此外,Gmail 提出了“不太安全”应用程序的概念,基本上任何人都可以使用普通密码登录 Gmail,因此您最终可能会遇到一个用户名可以发送邮件的情况(对“不太安全”的应用程序的支持是启用)但其他被阻止(禁用对“不太安全”的应用程序的支持)。您可以在此处配置您的 Gmail 帐户以允许安全性较低的应用程序。使用此方法时,请确保还通过完成“Captcha Enable”挑战来启用所需的功能。没有这个,安全性较低的连接可能无法工作。并确保“安全性较低的应用程序”选项已打开&nbsp;如果您不知道如何打开,可以访问此处有关更多信息,我建议您查看此页面
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript