如何在nodejs中发送电子邮件

时间:2016-09-12 10:36:08

标签: node.js email

我阅读了以下内容Sending emails in Node.js?,但我正在寻找发送电子邮件的方法,而不是通过smtp服务器。与linux环境一样,您有不同的选项,例如sendmail and others

我可以使用我所处的环境来利用已有的功能,但我有兴趣了解如何使用js来发送电子邮件,如果可能的话......

我使用smtp模块设置了一个smtp服务器:https://github.com/andris9/smtp-server为什么我对我已经设置的服务器的交付部分感兴趣。

4 个答案:

答案 0 :(得分:3)

看看节点邮件。您可以在没有smtp服务器的情况下进行设置。 https://github.com/nodemailer/nodemailer

答案 1 :(得分:0)

     var nodemailer = require('nodemailer');
     var send = require('gmail-send');
     var mailserverifo = nodemailer.createTransport({
     service: 'gmail',
     host : "smtp.gmail.com",
     port : "465",
     ssl : true,
     auth: {
     user: 'email@gmail.com',
     pass: 'password@'
    }
    });
      var Mailinfo = {
      from: 'email@gmail.com',
      to: 'email@info.com',
      subject: 'Testing email from node js server',
      text: 'That was easy!'
     };

    mailserverifo.sendMail(Mailinfo, function(error, info){
    if (error) {
     console.log(error);
    } else {
     console.log('Email Send Success: ' + info.response);
    }
    });

   Enable less secure app form setting -
   https://www.google.com/settings/security/lesssecureapps
   Disable Captcha  - 
   https://accounts.google.com/b/0/displayunlockcaptcha

答案 2 :(得分:0)

您可以在Node js中使用sendmail。我正在使用它,它对我来说很好。

npm install sendmail --save

const sendmail = require('sendmail')();
 
sendmail({
    from: 'no-reply@yourdomain.com',
    to: 'test@qq.com, test@sohu.com, test@163.com ',
    subject: 'test sendmail',
    html: 'Mail of test sendmail ',
  }, function(err, reply) {
    console.log(err && err.stack);
});

https://www.npmjs.com/package/sendmail

答案 3 :(得分:0)

首先:安装nodemailer
npm install nodemailer
然后将其放入您的节点文件中:

var nodemailer = require('nodemailer');
var http = require('http');
var url = require('url');
console.log("Creating Transport")
var transporter = nodemailer.createTransport({
    service:'Hotmail',
    auth: {
        user:'salace2008765@outlook.com',
        pass: 'alice123@'
   }
});
var mailOptions = {
    from:'salace2008765@outlook.com',
    to: 'jerome20090101@gmail.com',
    subject: 'This is a test: test',
    text:'TgK'
}
console.log("Sending mail")
transporter.sendMail(mailOptions, function(error, info) {
    if (error) {
        console.log(error);
    } else {
        console.log('Email sent: ' + info.response)
    }
})

通常有效 来源:W3SchoolsNodemailer's official site