使用nodemailer通过Node.js发送电子邮件不起作用

Spr*_*der 26 email smtp node.js nodemailer

我已经在本地(http://localhost:8080)设置了一个基本的NodeJS服务器(使用nodemailer模块),这样我就可以测试服务器是否可以实际发送电子邮件了.

如果我理解正确的SMTP选项(请纠正我,如果我错了),我可以尝试从我的服务器发送电子邮件给某人的电子邮件帐户直接,或者我可以发送电子邮件,仍然使用Node.js的,但通过实际的电子邮件帐户(在这种情况下是我的个人Gmail帐户),即使用SMTP.此选项要求我通过NodeJS远程登录该帐户.

因此,在下面的服务器中,我实际上是尝试使用NodeJ从我的个人电子邮件帐户向我的个人电子邮件帐户发送电子邮件.

这是我的简单服务器:

var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport("SMTP", {
    service: 'Gmail',
    auth: {
        user: '*my personal Gmail address*',
        pass: '*my personal Gmail password*'
    }
});

var http = require('http');
var httpServer = http.createServer(function (request, response)
{
    transporter.sendMail({
       from: '*my personal Gmail address*',
       to: '*my personal Gmail address*',
       subject: 'hello world!',
       text: 'hello world!'
    });
}).listen(8080);
Run Code Online (Sandbox Code Playgroud)

但是,它不起作用.我收到了谷歌的一封电子邮件说:

Google帐户:登录尝试已被阻止如果是您的话您可以切换到Google制作的应用程序(如Gmail)来访问您的帐户(推荐)或更改您的设置,网址https://www.google.com/settings/security/ lesssecureapps,以便您的帐户不再受现代安全标准的保护.

我在nodemailer GitHub页面上找不到上述问题的解决方案.有没有人有解决方案/建议?

谢谢!:-)

xSh*_*ase 55

答案来自谷歌的消息.

对于问题的第二部分,并作出回应

我实际上只是按照nodemailer github页面中的步骤操作,因此我的代码中没有错误

我将向您推荐nodemailer github页面,以及这段代码:

var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
    user: 'gmail.user@gmail.com',
    pass: 'userpass'
}
});
Run Code Online (Sandbox Code Playgroud)

它与你的代码略有不同,因为你有:nodemailer.createTransport("SMTP".删除SMTP参数,它工作(刚刚测试).另外,为什么将它封装在http服务器中?以下作品:

var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
    service: 'Gmail',
    auth: {
        user: 'xxx',
        pass: 'xxx'
    }
});

console.log('created');
transporter.sendMail({
from: 'xxx@gmail.com',
  to: 'xxx@gmail.com',
  subject: 'hello world!',
  text: 'hello world!'
});
Run Code Online (Sandbox Code Playgroud)


iro*_*ion 20

对于那些真正想要使用OAuth2 /不想让应用程序"不太安全"的人,你可以实现这一点

  1. Google API控制台中搜索"Gmail API" ,然后点击"启用"
  2. 按照https://developers.google.com/gmail/api/quickstart/nodejs中的步骤操作.在quickstart.js文件,改变了SCOPES从VAR ['https://www.googleapis.com/auth/gmail.readonly']['https://mail.google.com/']所提供的快速入门js文件作为提示故障排除https://nodemailer.com/smtp/oauth2/
  3. 下面(2)中,将所生成的JSON文件将包含的步骤之后acessToken,refreshTokenexpires在所需的属性为Nodemailer的OAuth2实例

这样您就可以使用OAuth2身份验证,如下所示

let transporter = nodemailer.createTransport({
    service: 'Gmail',
    auth: {
        type: 'OAuth2',
        user: 'user@example.com',
        clientId: '000000000000-xxx0.apps.googleusercontent.com',
        clientSecret: 'XxxxxXXxX0xxxxxxxx0XXxX0',
        refreshToken: '1/XXxXxsss-xxxXXXXXxXxx0XXXxxXXx0x00xxx',
        accessToken: 'ya29.Xx_XX0xxxxx-xX0X0XxXXxXxXXXxX0x',
        expires: 1484314697598
    }
});
Run Code Online (Sandbox Code Playgroud)

而不是以明文形式存储您的Gmail密码并降低您帐户的安全性.

  • 不再有效,该文件中不再存在accessToken和refreshToken (2认同)
  • 为了让它工作,我使用了这个答案传输定义(没有指定 accessToken 和 expires)和这篇使用谷歌的 oauth 游乐场来获取刷新令牌的帖子的组合:https://medium.com/@pandeysoni/nodemailer-service -in-node-js-using-smtp-and-xoauth2-7c638a39a37e (2认同)
  • @CaptRisky回复:您的评论“该文件中不再存在accessToken和refreshToken”是完全不正确的。我按照说明进行操作,确实确实产生了一个具有所有值的token.json文件:access_token,refresh_token,scope,token_type和expiry_date。 (2认同)

Mat*_*ini 11

我只是将我的域名设置为:smtp.gmail.com,它的工作原理.我正在使用VPS Vultr.

代码:

const nodemailer = require('nodemailer');
const ejs = require('ejs');
const fs = require('fs');

let transporter = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    port: 465,
    secure: true,
    auth: {
        user: 'xxx@gmail.com',
        pass: 'xxx'
    }
});

let mailOptions = {
    from: '"xxx" <xxx@gmail.com>',
    to: 'yyy@gmail.com',
    subject: 'Teste Templete ?',
    html: ejs.render( fs.readFileSync('e-mail.ejs', 'utf-8') , {mensagem: 'olá, funciona'})
};

transporter.sendMail(mailOptions, (error, info) => {
    if (error) {
        return console.log(error);
    }
    console.log('Message %s sent: %s', info.messageId, info.response);
});
Run Code Online (Sandbox Code Playgroud)

我的ejs模板(电子邮件.ejs):

<html>
    <body>
        <span>Esse é um templete teste</span>
        <p> gerando com o EJS - <%=mensagem%> </p>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

确保:

  • 安装ejs:npm install ejs --save
  • 安装nodemailer:npm install nodemailer --save
  • ping到smtp.gmail.com的工作原理:ping smtp.gmail.com
  • 将xxx@gmail.com更改为您的Gmail电子邮件
  • 将yyy@gmail.com更改为您要发送电子邮件的电子邮件
  • 启用不太安全的应用
  • 暂时禁用验证码

祝你今天愉快 ;)


Ahm*_*ais 10

虽然上述答案确实有效,但我想指出您可以通过以下两个步骤来降低 Gmail 的安全性。

第1步

Google 帐户:登录尝试被阻止 如果是您您可以切换到 Google 制作的应用程序(例如 Gmail)来访问您的帐户(推荐)或在https://www.google.com/settings/security/更改您的设置lesssecureapps使您的帐户不再受现代安全标准的保护。

第2步

除了启用允许不太安全的应用程序外,您可能还需要导航到https://accounts.google.com/DisplayUnlockCaptcha并单击继续。

  • 这是缺少的步骤:https://accounts.google.com/DisplayUnlockCaptcha 谢谢!! (2认同)

kdg*_*ang 5

您只需要 google auth 的 App 密码,然后在您的代码中替换您的 google 密码。去这里https://myaccount.google.com/apppasswords

示例代码:

const nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
    service: "Gmail",
    auth: {
      user: 'example@gmail.com',
      pass: 'app password here'
    }
  });
transporter.sendMail(option, function(error, info){
    if (error) {
      console.log(error);
    } else {
      console.log('Email sent: ' + info.response);
    }
});
Run Code Online (Sandbox Code Playgroud)

截屏

  • 不知道为什么,但这适用于本地主机,而不适用于我的登台环境 (2认同)
  • @HarpreetSingh 嗨,它可以在蔚蓝的云环境中工作吗?对我来说,它也在本地工作,但不在云中。如果你能更新这个对我来说非常有用.. (2认同)