我正在尝试使用emailjs(https://github.com/eleith/emailjs)发送带有nodejs的电子邮件,但是,它给了我一个错误:{[错误:连接到smtp服务器时超时]代码:4,smtp:undefined }
我正在尝试使用hotmail,但我可以使用其他,我只想要这个工作。好吗?
var email = require("emailjs");
var server = email.server.connect({
user: "xxx@hotmail.com",
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 <xxxxxx@hotmail.com>",
to: "someone <zzzzz@hotmail.com>",
//cc: "else <else@your-email.com>",
subject: "testing emailjs"
}, function(err, message) { console.log(err || message); });
答案 0 :(得分:3)
我使用下面的代码测试(使用gmail)并且它正在运行:
var email = require('emailjs');
var server = email.server.connect({
user: 'nodejsiscool@gmail.com',
password: 'stackoverflow',
host: 'smtp.gmail.com',
ssl: true
});
server.send({
text: 'Hey howdy',
from: 'NodeJS',
to: 'Wilson <wilson.balderrama@gmail.com>',
cc: '',
subject: 'Greetings'
}, function (err, message) {
console.log(err || message);
});
在我的控制台中,输出为:
{
attachments: [],
alternative: null,
header: {
'message-id': '<1433256538447.0.5970@Wilsons-MacBook-Pro.local>',
date: 'Tue, 02 Jun 2015 10:48:58 -0400',
from: '=?UTF-8?Q?NodeJS?= <>',
to: '=?UTF-8?Q?Wilson?= <wilson.balderrama@gmail.com>',
cc: '',
subject: '=?UTF-8?Q?Greetings?='
},
content: 'text/plain; charset=utf-8',
text: 'Hey howdy'
}
事实上,我收到了收件箱中的电子邮件信息。
我怀疑你应该使用主机 smtp.live.com 而不是 smtp-mail.live.com
注意:使用的帐户(nodejsiscool@gmail.com)是我刚刚为测试目的而创建的有效帐户。
答案 1 :(得分:2)
使用Gmail
首先:如Gmail文档中所示,必须使用安全性较低的应用程序,风险自负。我使用的帐户对我的测试不是很重要:
https://myaccount.google.com/u/2/lesssecureapps?pageId=none
<强>后:强>
var email = require("emailjs");
var server = email.server.connect({
user: "YOUR_ACCOUNT@gmail.com",
password:"YOUR_PASSWORD",
host: "smtp.gmail.com",
ssl: true
});
server.send({
text: "Hello world",
from: "YOUR_NAME <YOUR_ACCOUNT@gmail.com>",
to: "FRIEND_NAME <FRIEND_ACCOUNT@gmail.com>",
subject: "Hello"
}, function(err, message) { console.log(err || message); });
附加说明:关于在Gmail收件箱中设置“来自”和显示名称的方式:
示例1:
仅使用名称设置“from”。
from: "YOUR_NAME", // Only name
在收件箱显示中&lt;&gt;
YOUR_NAME <> Hello 18:11
示例2:
使用名称和emial设置“from”。
from: "YOUR_NAME <YOUR_ACCOUNT@gmail.com>", // Name and email
在收件箱中不显示&lt;&gt;
YOUR_NAME Hello 18:11
答案 2 :(得分:0)
确保正确链接emailjs文件(查看var email),否则无效。
//Email stuff
var email = require("./node_modules/emailjs/email");
var server = email.server.connect({
user: "EMAIL@gmail.com",
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@gmail.com>',
cc: '',
subject: ''
}, function (err, message) {
console.log(err || message);
});
答案 3 :(得分:0)
确保您已安装具有相同版本的确切软件包
npm i emailjs@1.0.11
并尝试以下代码
var email = require("emailjs");
var server = email.server.connect({
user: "your-email@gmail.com",
password: "your-password",
host: "smtp.gmail.com",
ssl: true
});
server.send(
{
text: "Hey howdy",
from: "NodeJS",
to: "Wilson <your-email@gmail.com>",
cc: "",
subject: "Greetings"
},
function(err, message) {
console.log(err || message);
}
);
似乎使用es6的最新版本无法正常工作,但会导出“ emailjs@1.0.11”,但该版本仍然可以正常工作。 也不要忘记为您使用密码的电子邮件地址启用less secure设置。