首页 > 解决方案 > 使用 gmail 凭据从节点服务器发送电子邮件

问题描述

我希望能够从我的节点服务器发送电子邮件。我创建了一个google developer project并启用了gmail api它。gmail api之后,我以 json 格式下载了凭据。但我无法查找nodemailer文档,因为该站点似乎已关闭。

如何使用我下载的 gmail 凭据从我的节点服务器发送电子邮件?

标签: node.jsrestservergmail

解决方案


这是使用 nodemailer 发送邮件的示例代码。使用以下代码创建快递应用程序。

 var nodemailer = require("nodemailer");
    var smtpTransport = nodemailer.createTransport("SMTP", {
    host: 'smtp.gmail.com',
    port: 587,
    auth: {
      user: 'test@gmail.com', //Gamilid that was created by you
      pass: 'password'
    },
    secure: true
  });


  // app.get('/send',function(req,res){
  var mailOptions = {
    to: 'sampleto@testcom',
    subject: "MAIL TEST",
    text: "Hi this is the test mail"
  }
  console.log(mailOptions);


  smtpTransport.sendMail(mailOptions, function (error, response) {
    if (error) {
      console.log(error);
      res.end("error");
    } else {
      console.log("Message sent: " + response.message);
      res.end("sent");
    }
  });

推荐阅读