电子邮件NodeJS无法发送 [英] Email NodeJS cannot send
问题描述
我想从NodeJs发送电子邮件.我在npmjs.org上进行了快速搜索,发现了2个有前途的库:
I want to send an email from NodeJs. I did a quick search on npmjs.org and found 2 promising libraries:
和
首先,我按照说明安装和运行NodeMailer,但是它不起作用.我想从Gmail发送电子邮件,因此,我启用了安全性较低" 应用以及
First, I followed the instructions to install and run NodeMailer, but It does not work. I wanted to send email from Gmail, thus, I enabled the "less secured" apps and also enabled captcha as suggested in the documentation of Nodemailer. However, even though I did this I could not make it work.
此外,我尝试切换到大学的电子邮件,因此找到了它的SMTP服务器和端口并配置了设置.该错误仍然存在,主要是:
In addition, I tried to switch to my University's email, so I found its SMTP server and Port and configured the settings. The error persisted, mainly:
(node:825) UnhandledPromiseRejectionWarning: Unhandled promise rejection
(rejection id: 1): Error: connect EAGAIN 134.91.56.35:587 - Local (0.0.0.0:58442)
(node:825) [DEP0018] DeprecationWarning: Unhandled promise rejections are
deprecated. In the future, promise rejections that are not handled will
terminate the Node.js process with a non-zero exit code.
我不确定如何解决它,因此我尝试了下一个客户端EmailJs.那里的错误是:
I am not exactly sure how to fix it, so I tried the next client, EmailJs. The error there is:
{ Error: connection encountered an error (connect EAGAIN 134.91.56.35:587 ...
code: 5,
previous: ...
... smtp: undefied}
我可以清楚地看到,上面写着smtp是未定义的,但是我已经证实它是相同的.
I can clearly see that it is written that smtp is undefined, but I have verified that it is the same.
为了澄清,我在Windows的Bash上运行NodeJ,但是由于我知道它存在一些问题,因此我也在Raspberry PI 3上运行了代码.
To clarify, I am running NodeJs on Bash on Windows, but since I know there are some issues with it I also run the code on a Raspberry PI 3.
谢谢!
推荐答案
我设法对其进行了配置.
I managed to configure it.
根据建议,我使用了 Gmail节点.为此,您必须执行一些步骤:
As suggested I used Gmail-node. To do this you have to follow some steps:
- 首先,使用
npm install gmail-node
安装它 - 打开链接 https://console.developers.google.com/flows/enableapi?apiid = gmail
- 使用此向导在Google Developers中创建或选择一个项目控制台并自动打开API.
- 单击继续,然后转到凭据.
- 在页面顶部,选择OAuth同意屏幕标签.选择一个电子邮件地址,如果尚未输入产品名称,请输入设置,然后单击保存"按钮.
- 选择凭据"选项卡,然后单击创建凭据按钮,然后选择OAuth客户端ID.
- 选择应用程序类型其他",输入任意名称"gmail-node-app",然后单击创建按钮.
- 单击确定"关闭出现的对话框.
- 点击文件右侧的file_download(下载JSON)按钮客户编号.
- 将此文件移动到您的工作目录中,并与任何名称,例如client_secret.json.
现在您的程序将如下所示,您必须根据刚刚下载的文件来更改信息:
Now your program would look like this where you have to change the information in accordance to the file you just downloaded:
var gmailNode = require('gmail-node');
var clientSecret = {
installed: {
client_id: "k677725446467-6li25pcqgkcllsoh6f6dijcvse64n9pf.apps.googleusercontent.com",
project_id: "clean-node-129606",
auth_uri: "https://accounts.google.com/o/oauth2/auth",
token_uri: "https://accounts.google.com/o/oauth2/token",
auth_provider_x509_cert_url: "https://www.googleapis.com/oauth2/v1/certs",
client_secret: "kF7DvoA_ZrNfa65GnU2zQBgw7",
redirect_uris: [
"urn:ietf:wg:oauth:2.0:oob",
"http://localhost"
]
}
};
gmailNode.init(clientSecret, './token.json', function(err,data){ ... });
但是,从网站上获取此信息后,我仍然遇到一个未知的问题,即错误:未设置访问或刷新令牌.
.
However, after taking this from the website, I still had an unknown problem, namely Error: No access or refresh token is set.
.
为了解决该问题,您需要通过记录以下代码来生成带有"TokenURL"的网址:
In order to solve it, you need to generate url with a "TokenURL" by log this code:
console.log(gmailNode.generateUrl(clientSecret));
跟随生成的URL,然后在其中复制"TokenURL".
Follow the generated URL and copy the "TokenURL" there.
最后,通过以下方式发送电子邮件:
Lastly, send an email with:
gmailNode.generateToken(clientSecret, tokenURL, (err, data) => {
gmailNode.sendWithToken(emailMessage, clientSecret, data, function(err,data) {
console.log(err, data)
})
})
您完成了!
请注意,我们不再需要
init
.
这篇关于电子邮件NodeJS无法发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!