javascript emailjs 不工作 [节点 js]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30598980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
emailjs not working [node js]
提问by JMR
i'm trying to use the emailjs (https://github.com/eleith/emailjs) for send emails with nodejs, but, it gives me an error: { [Error: timedout while connecting to smtp server] code: 4, smtp: undefined }
我正在尝试使用 emailjs ( https://github.com/eleith/emailjs) 通过 nodejs 发送电子邮件,但是,它给了我一个错误:{ [错误:连接到 smtp 服务器时超时] 代码:4, smtp: 未定义 }
I'm trying to use hotmail, but I can use other, I only want this working. Any idea please?
我正在尝试使用 hotmail,但我可以使用其他的,我只希望它工作。请问有什么想法吗?
var email = require("emailjs");
var server = email.server.connect({
user: "[email protected]",
password:"xxxyyyy",
host: "smtp-mail.live.com",
ssl: true
});
// send the message and get a callback with an error or details of the message that was sent
server.send({
text: "i hope this works",
from: "you <[email protected]>",
to: "someone <[email protected]>",
//cc: "else <[email protected]>",
subject: "testing emailjs"
}, function(err, message) { console.log(err || message); });
采纳答案by Wilson
I tested with the code below (using gmail) and it is working:
我用下面的代码(使用 gmail)进行了测试,它正在工作:
var email = require('emailjs');
var server = email.server.connect({
user: '[email protected]',
password: 'stackoverflow',
host: 'smtp.gmail.com',
ssl: true
});
server.send({
text: 'Hey howdy',
from: 'NodeJS',
to: 'Wilson <[email protected]>',
cc: '',
subject: 'Greetings'
}, function (err, message) {
console.log(err || message);
});
In my console the output is:
在我的控制台中,输出是:
{
attachments: [],
alternative: null,
header: {
'message-id': '<[email protected]>',
date: 'Tue, 02 Jun 2015 10:48:58 -0400',
from: '=?UTF-8?Q?NodeJS?= <>',
to: '=?UTF-8?Q?Wilson?= <[email protected]>',
cc: '',
subject: '=?UTF-8?Q?Greetings?='
},
content: 'text/plain; charset=utf-8',
text: 'Hey howdy'
}
Indeed I have received in my inbox the email message.
事实上,我已经在我的收件箱中收到了电子邮件。
I suspect that you should use the host smtp.live.cominstead of smtp-mail.live.com
我怀疑您应该使用主机smtp.live.com而不是smtp-mail.live.com
Note: The account ([email protected]) used is a valid one I just created for testing purposes.
注意:所使用的帐户 ([email protected]) 是我刚刚创建的用于测试目的的有效帐户。
回答by alditis
Using Gmail
使用 Gmail
First: Required activate the use less secure apps at your own risk as indicated in the Gmail documentation. I use an account not very important for my tests:
第一:需要激活使用安全性较低的应用程序,风险自负,如 Gmail 文档中所述。我使用的帐户对我的测试不是很重要:
https://myaccount.google.com/u/2/lesssecureapps?pageId=none
https://myaccount.google.com/u/2/lesssecureapps?pageId=none
After:
后:
var email = require("emailjs");
var server = email.server.connect({
user: "[email protected]",
password:"YOUR_PASSWORD",
host: "smtp.gmail.com",
ssl: true
});
server.send({
text: "Hello world",
from: "YOUR_NAME <[email protected]>",
to: "FRIEND_NAME <[email protected]>",
subject: "Hello"
}, function(err, message) { console.log(err || message); });
Additional Note:About how set "from" and displayed name in Gmail inbox:
附加说明:关于如何在 Gmail 收件箱中设置“发件人”和显示名称:
Example 1:
示例 1:
Set "from" with only name.
仅使用名称设置“来自”。
from: "YOUR_NAME", // Only name
In inbox display <>
在收件箱显示 <>
YOUR_NAME <> Hello 18:11
Example 2:
示例 2:
Set "from" with name and emial.
使用名称和 emial 设置“来自”。
from: "YOUR_NAME <[email protected]>", // Name and email
In inbox not display <>
在收件箱中不显示 <>
YOUR_NAME Hello 18:11
回答by Fadi Bitar
Make sure you are linking the emailjs files correctly (check var email) or it won't work.
确保您正确链接了 emailjs 文件(检查 var email),否则它将无法工作。
//Email stuff
var email = require("./node_modules/emailjs/email");
var server = email.server.connect({
user: "[email protected]",
password:"INSERTPASSWORDHERE",
host: "smtp.gmail.com",
ssl: true
});
//If button is clicked then send email
server.send({
text: 'Hello World! \n\n Regards,\n INSERTNAMEHERE \n',
from: 'Name',
to: 'Name <[email protected]>',
cc: '',
subject: ''
}, function (err, message) {
console.log(err || message);
});