简体   繁体   English

使用 nodemailer 在 node.js 中发送邮件

[英]Sending mail in node.js using nodemailer

I am trying to send mail in node.js using Nodemailer but it shows some error like { [Error: self signed certificate in certificate chain] code: 'ECONNECTION', command: 'CONN' }我正在尝试使用 Nodemailer 在 node.js 中发送邮件,但它显示了一些错误,例如{ [Error: self signed certificate in certificate chain] code: 'ECONNECTION', command: 'CONN' }

My node.js code is我的 node.js 代码是

var express    =    require('express');
var app        =    express();
var nodemailer = require('nodemailer');

var transporter = nodemailer.createTransport('smtps://something%40gmail.com:password@smtp.gmail.com');

var mailOptions = {
  to: 'stevecameron2016@gmail.com',
  subject: 'Hello ?', 
  text: 'Hello world ??', 
  html: '<b>Hello world ??</b>' 
};

transporter.sendMail(mailOptions, function(error, info){
  if(error){
     return console.log(error);
  }
  console.log('Message sent: ' + info.response);
});

var server     =    app.listen(8900,function(){
  console.log("We have started our server on port 8900");
});

try https://github.com/nodemailer/nodemailer/issues/406试试https://github.com/nodemailer/nodemailer/issues/406

add tls: { rejectUnauthorized: false } to your transporter constructor optionstls: { rejectUnauthorized: false }到您的传输器构造函数选项中

ps It's not a good idea to post your mail server address, if it's a real one ps 如果是真实的,发布您的邮件服务器地址不是一个好主意

To allow to send an email via “less secure apps”, go to the link and choose “Turn on”.要允许通过“不太安全的应用程序”发送电子邮件,请转到链接并选择“打开”。

( More info about less secure apps) (有关不太安全的应用程序的更多信息

var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');

var mailAccountUser = '<YOUR_ACCOUNT_USER>'
var mailAccountPassword = '<YOUR_ACCOUNT_PASS>'

var fromEmailAddress = '<FROM_EMAIL>'
var toEmailAddress = 'TO_EMAIL'

var transport = nodemailer.createTransport(smtpTransport({
    service: 'gmail',
    auth: {
        user: mailAccountUser,
        pass: mailAccountPassword
    }
}))

var mail = {
    from: fromEmailAddress,
    to: toEmailAddress,
    subject: "hello world!",
    text: "Hello!",
    html: "<b>Hello!</b><p><a href=\"http://www.yahoo.com\">Click Here</a></p>"
}

transport.sendMail(mail, function(error, response){
    if(error){
        console.log(error);
    }else{
        console.log("Message sent: " + response.message);
    }

    transport.close();
});

i was in this trouble too, what i did is next code line:我也遇到了这个麻烦,我所做的是下一行代码:

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

just before creating the smpttransport就在创建 smpttransport 之前

for example, in your code just put this:例如,在您的代码中只需输入:

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; var transporter = nodemailer.createTransport('smtps://something%40gmail.com:password@smtp.gmail.com');

what @user3985565 said is correct. @user3985565 说的是正确的。 However , if you are using gmail you also need to change some settings in your gmail account.但是,如果您使用的是 gmail,您还需要更改您的 gmail 帐户中的一些设置。 More specifically you need to "allow less secure apps" in you gmail account.更具体地说,您需要在您的 Gmail 帐户中“允许安全性较低的应用程序”。 To do that just follow these steps:为此,只需按照以下步骤操作:

  1. test nodemailer as it is按原样测试 nodemailer
  2. node will throw an error and gmail will send you a security alert email informing you that "an unsafe app tried to access your account" node 会抛出错误,gmail 会向您发送一封安全警报电子邮件,通知您“一个不安全的应用程序试图访问您的帐户”
  3. in this email you need to click on "check activity" and then, in the folowing screen, you must unswer "yes"在这封电子邮件中,您需要单击“检查活动”,然后在以下屏幕中,您必须取消“是”
  4. Your next clicks in the following screens are "more information" and then "less secure apps".您在以下屏幕中的下一次点击是“更多信息”,然后是“安全性较低的应用程序”。
  5. finally you will see a toggle switch and you must turn it on.最后你会看到一个切换开关,你必须打开它。

it worked for me.它对我有用。

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM