从我的 ubuntu (10.04) 盒子,我可以毫无问题地发送电子邮件:
echo "hello" | mail -s 'test email' my_gmail_nickname@gmail.com
当我尝试从同一台机器上运行的 node.js 应用程序发送电子邮件时,它不起作用。
var nodemailer = require('nodemailer');
nodemailer.SMTP = {
host: 'localhost'
}
nodemailer.send_mail(
{
sender: 'me@example.com',
to:'my_gmail_nickname@gmail.com',
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
我看到连接被拒绝,但不明白为什么会出现此错误。您认为缺少的部分是什么?
最佳答案
我觉得你的问题是这样的:
命令行程序 mail 使用名为 /usr/sbin/sendmail 的二进制文件来发送邮件。 sendmail 是一个命令行程序,它将尝试发送邮件。它使用与邮件基础设施的本地连接。
Node nodemailer 将尝试连接到主机 localhost 上不存在的 TCP 端口 25 上的 SMTP 服务器。只需尝试使用 telnet 程序建立连接以进行验证。
这是一个正在运行的服务器:
$ telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 xxxx.de ESMTP Postfix
QUIT
221 2.0.0 Bye
Connection closed by foreign host.
这里没有服务器运行:
$ telnet localhost 25
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused
如果你得到第二个,你的 SMTP 有问题,它没有启动/启用监听端口 25 - 这是默认设置(出于安全原因)。您需要先配置它。
或者 - 根据 nodemail 文档,您也可以使用 sendmail 二进制文件:
'sendmail' alternative Alternatively if you don't want to use SMTP but the sendmail
command then set property sendmail to true (or as the path to sendmail if the command is not in default path).
nodemailer.sendmail = true; or nodemailer.sendmail = '/path/to/sendmail'; If sendmail is set, then SMTP options are discarded.
关于node.js - 使用 nodemailer 从 node.js 应用程序发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7105045/