使用 nodemailer 在 node.js 中发送邮件 [英] Sending mail in node.js using nodemailer

查看:62
本文介绍了使用 nodemailer 在 node.js 中发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Nodemailer 在 node.js 中发送邮件,但它显示了一些错误,例如{ [错误:证书链中的自签名证书] 代码:'ECONNECTION',命令:'CONN' }

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' }

我的 node.js 代码是

My node.js code is

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

推荐答案

要允许通过不太安全的应用"发送电子邮件,请转到 链接,然后选择打开".

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

这篇关于使用 nodemailer 在 node.js 中发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆