使用nodemailer从node.js应用程序发送电子邮件
从我的 ubuntu (10.04) 盒子中,我可以通过以下方式发送电子邮件:
echo "hello" | mail -s 'test email' [email protected]
当我尝试从同一台计算机上运行的 node.js 应用程序发送电子邮件时,它不起作用。
var nodemailer = require('nodemailer');
nodemailer.SMTP = {
host: 'localhost'
}
nodemailer.send_mail(
{
sender: '[email protected]',
to:'[email protected]',
subject:'Hello!',
html: 'test',
body:'test'
},
function(error, success){
console.log(error);
console.log(success);
console.log('Message ' + success ? 'sent' : 'failed');
});
我收到错误消息:
me@luc:~/gridteams/services/gpshop$ cat nohup.out
{ stack: [Getter/Setter],
arguments: undefined,
type: undefined,
message: 'ECONNREFUSED, Connection refused',
errno: 111,
code: 'ECONNREFUSED',
syscall: 'connect' }
null
sent
我看到连接被拒绝,但不明白为什么会收到此错误。您认为缺失的部分是什么?
From my ubuntu (10.04) box, I have no problem send email with:
echo "hello" | mail -s 'test email' [email protected]
When I try to send an email from a node.js app running on the same machine, it does not work.
var nodemailer = require('nodemailer');
nodemailer.SMTP = {
host: 'localhost'
}
nodemailer.send_mail(
{
sender: '[email protected]',
to:'[email protected]',
subject:'Hello!',
html: 'test',
body:'test'
},
function(error, success){
console.log(error);
console.log(success);
console.log('Message ' + success ? 'sent' : 'failed');
});
I have the error message:
me@luc:~/gridteams/services/gpshop$ cat nohup.out
{ stack: [Getter/Setter],
arguments: undefined,
type: undefined,
message: 'ECONNREFUSED, Connection refused',
errno: 111,
code: 'ECONNREFUSED',
syscall: 'connect' }
null
sent
I see the connection refused but do not understand why I get this error. What do you think is the missing piece ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你的问题是这样的:
命令行程序 mail 使用名为 /usr/sbin/sendmail 的二进制文件来发送邮件。 sendmail 是一个命令行程序,它将尝试传递邮件。它使用到邮件基础设施的本地连接。
节点nodemailer将尝试连接到主机localhost上的TCP端口25上的SMTP服务器,但该服务器不存在。只需尝试使用 telnet 程序建立连接进行验证即可。
这是一个正在运行的服务器:
这里没有正在运行的服务器:
如果您得到第二个,则说明您的 SMTP 有问题,该 SMTP 未启动/启用在端口 25 上侦听 - 这是默认值(出于安全原因)。您需要先配置它。
或者 - 根据 nodemail 文档,您也可以使用 sendmail 二进制文件:
I think your problem is like this:
The commandline program mail uses a binary called /usr/sbin/sendmail for sending mail. sendmail is a commandline programm which will try to deliver the mail. It uses a local connection to the mail infrastructure.
The node nodemailer will try to connect to a SMTP server on the host localhost on TCP port 25 which doesn't exist. Just try to get a connection using the telnet program for verification.
Here is a server running:
here is no server running:
If you get the second, you have a problem with your SMTP which is not started/enabled listening on port 25 - which is the default (for security reasons). You need to configure it first.
Or - according to the nodemail documentation you can use the sendmail binary as well: