Vin*_*243 19 email node.js nodemailer
我试图通过nodemailer发送电子邮件,而无需SMTP传输.所以我做到了:
var mail = require("nodemailer").mail;
mail({
from: "Fred Foo ? <foo@blurdybloop.com>", // sender address
to: "******@gmail.com", // list of receivers
subject: "Hello ?", // Subject line
text: "Hello world ?", // plaintext body
html: "<b>Hello world ?</b>" // html body
});
Run Code Online (Sandbox Code Playgroud)
但是当我跑步时,我得到了:
> node sendmail.js
Queued message #1 from foo@blurdybloop.com, to vinz243@gmail.com
Retrieved message #1 from the queue, reolving gmail.com
gmail.com resolved to gmail-smtp-in.l.google.com for #1
Connecting to gmail-smtp-in.l.google.com:25 for message #1
Failed processing message #1
Message #1 requeued for 15 minutes
Closing connection to the server
Error: read ECONNRESET
at errnoException (net.js:901:11)
at TCP.onread (net.js:556:19)
Run Code Online (Sandbox Code Playgroud)
我在Windows 7 32上.
编辑 这似乎是一个Windows相关的错误,它在Linux上工作
编辑#2
在git shell上,如果我输入telnet smtp.gmail 587它在这里被阻止:
220 mx.google.com ESMTP f7...y.24 -gsmtp
Run Code Online (Sandbox Code Playgroud)
Ris*_*vik 14
从您的示例输出似乎连接到错误的端口25,打开的gmail smtp端口是465用于SSL和其他587 TLS.
Nodemailer会根据电子邮件域检测到正确的配置,在您的示例中,您尚未设置传输器对象,因此它使用配置的默认端口25.要更改端口,请在选项中指定类型.
这是应该与gmail一起使用的小例子:
var nodemailer = require('nodemailer');
// Create a SMTP transport object
var transport = nodemailer.createTransport("SMTP", {
service: 'Gmail',
auth: {
user: "test.nodemailer@gmail.com",
pass: "Nodemailer123"
}
});
console.log('SMTP Configured');
// Message object
var message = {
// sender info
from: 'Sender Name <sender@example.com>',
// Comma separated list of recipients
to: '"Receiver Name" <nodemailer@disposebox.com>',
// Subject of the message
subject: 'Nodemailer is unicode friendly ?',
// plaintext body
text: 'Hello to myself!',
// HTML body
html:'<p><b>Hello</b> to myself <img src="cid:note@node"/></p>'+
'<p>Here\'s a nyan cat for you as an embedded attachment:<br/></p>'
};
console.log('Sending Mail');
transport.sendMail(message, function(error){
if(error){
console.log('Error occured');
console.log(error.message);
return;
}
console.log('Message sent successfully!');
// if you don't want to use this transport object anymore, uncomment following line
//transport.close(); // close the connection pool
});
Run Code Online (Sandbox Code Playgroud)
使用nodemailer 0.7.1.
如果您之前安装了nodemailer,请将其删除
npm remove nodemailer
Run Code Online (Sandbox Code Playgroud)
现在安装它
npm install nodemailer@0.7.1
Run Code Online (Sandbox Code Playgroud)
以下代码在没有登录的情况下发送邮件,但它只能在生产环境中工作,它不能在本地工作
var mail = require("nodemailer").mail;
mail({
from: "Fred Foo ? <foo@blurdybloop.com>", // sender address
to: "bar@blurdybloop.com, baz@blurdybloop.com", // list of receivers
subject: "Hello ?", // Subject line
text: "Hello world ?", // plaintext body
html: "<b>Hello world ?</b>" // html body
});
Run Code Online (Sandbox Code Playgroud)
看起来当前nodemailer没有选择再发送没有smtp的邮件.我会建议采取一看的sendmail这确实库不是需要任何SMTP/AUTH发送电子邮件.它为您提供了在linux中使用sendmail的类似体验.
const sendmail = require('sendmail')();
sendmail({
from: 'test@finra.org',
to: 'YourName@gmail.com',
subject: 'Hello World',
html: 'Hooray NodeJS!!!'
}, function (err, reply) {
console.log(err && err.stack)
console.dir(reply)
})
Run Code Online (Sandbox Code Playgroud)
你也可以打开silent它以减少冗长:const sendmail = require('sendmail')({silent: true});.有一点要注意的是发送者不能是具有这些领域DMARC 的政策一样xxx@capitalone.com.
可能是 Windows 防火墙或防病毒软件阻止了传出访问。尝试启用 nodemailer 调试消息。
启用调试
var nodemailer = require("nodemailer"),
transport = nodemailer.createTransport('direct', {
debug: true, //this!!!
});
transport.sendMail({
from: "Fred Foo ? <foo@blurdybloop.com>", // sender address
to: "******@gmail.com", // list of receivers
subject: "Hello ?", // Subject line
text: "Hello world ?", // plaintext body
html: "<b>Hello world ?</b>" // html body
}, console.error);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
38356 次 |
| 最近记录: |