使用 nodemailer 通过 Node.js 发送电子邮件不起作用 [英] Sending email via Node.js using nodemailer is not working

查看:45
本文介绍了使用 nodemailer 通过 Node.js 发送电子邮件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在本地 (http://localhost:8080) 设置了一个基本的 NodeJS 服务器(使用 nodemailer 模块),只是为了测试服务器是否可以真正发送电子邮件.

I've set up a basic NodeJS server (using the nodemailer module) locally (http://localhost:8080) just so that I can test whether the server can actually send out emails.

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

If I understand the SMTP option correctly (please correct me if I'm wrong), I can either try to send out an email from my server to someone's email account directly, or I can send the email, still using Node.js, but via an actual email account (in this case my personal Gmail account), i.e using SMTP. This option requires me to login into that acount remotely via NodeJS.

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

So in the server below I'm actually trying to use NodeJs to send an email from my personal email account to my personal email account.

这是我的简单服务器:

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);

但是,它不起作用.我收到了一封来自 Google 的电子邮件,内容是:

However, it's not working. I got an email by Google saying :

Google 帐户:登录尝试被阻止如果这是你您可以切换到 Gmail 等 Google 出品的应用来访问您的帐户(推荐)或更改您在 https://www.google.com/settings/security/lesssecureapps 上的设置,以便您的帐户没有更长时间地受到现代安全标准的保护.

Google Account: sign-in attempt blocked If this was you You can switch to an app made by Google such as Gmail to access your account (recommended) or change your settings at https://www.google.com/settings/security/lesssecureapps so that your account is no longer protected by modern security standards.

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

I couldn't find a solution for the above problem on the nodemailer GitHub page. Does anyone have a solution/suggestion ?

谢谢!:-)

推荐答案

答案在来自 google 的消息中.

The answer is in the message from google.

访问不太安全的应用设置为启用

对于问题的第二部分,以及对

For the second part of the problem, and in response to

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

I'm actually simply following the steps from the nodemailer github page so there are no errors in my code

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

I will refer you to the nodemailer github page, and this piece of code :

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

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

It differs slightly from your code, in the fact that you have : nodemailer.createTransport("SMTP". Remove the SMTP parameter and it works (just tested). Also, why encapsulating it in a http server? the following works :

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!'
});

这篇关于使用 nodemailer 通过 Node.js 发送电子邮件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆