node.js - Nodemailer 无需 smtp 传输即可发送电子邮件

标签 node.js email nodemailer

我正在尝试通过没有 SMTP 传输的 nodemailer 发送电子邮件。所以我做到了:

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
});

但是当我运行时,我明白了:

> 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)

我在 Windows 7 32 上。

编辑 这似乎是一个与 Windows 相关的错误,因为它在 linux 上工作

编辑#2

在 git shell 上,如果我输入 telnet smtp.gmail 587 它会在此处被阻止:

220 mx.google.com ESMTP f7...y.24 -gsmtp

最佳答案

从您的示例输出看来,它似乎连接到了错误的端口 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
});

关于node.js - Nodemailer 无需 smtp 传输即可发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23016208/

相关文章:

javascript - fs.readdir 首先同步目录

javascript - Puppeteer:在异步函数中使用await 调用的javascript 函数中抛出自定义错误消息

node.js - 在 MongoDB 中按 INC 值查找文档

ruby - 正则表达式在电子邮件的原始正文中查找 Base64 (Ruby)

email - 从 Firebase webapp 发送邮件

javascript - 使用 nodemailer 和 smtp 无需身份验证发送邮件

javascript - Socket.io自动超时和传输错误

css - 如何防止 Gmail 应用程序解析地址并将链接插入到我的电子邮件正文中?

c#生成的csv文件通过电子邮件发送,嵌入到lotus note中电子邮件的底部

javascript - 使用 Node.js 和 Express.js 以及 AngularJs 和 nodemailer 发送电子邮件不发送邮件