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

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

问题描述

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

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.

所以在下面的服务器中,我实际上是试图使用NodeJ从我的个人电子邮件帐户发送一个电子邮件到我的个人电子邮件帐户。

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帐户:登录尝试被阻止
如果这是你
您可以切换到Google制作的应用程序(如Gmail)访问您的帐户(推荐)或更改
您的设置, 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.

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

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

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