nodejs發送郵件


這里我主要使用的是 nodemailer 這個插件

第一步 下載依賴

cnpm install nodemailer --save

第二步 建立email.js

'use strict';

const nodemailer = require('nodemailer');

let transporter = nodemailer.createTransport({
  // host: 'smtp.ethereal.email',
  service: 'qq', // 使用了內置傳輸發送郵件 查看支持列表:https://nodemailer.com/smtp/well-known/
  port: 465, // SMTP 端口
  secureConnection: true, // 使用了 SSL
  auth: {
    user: 'xxxxxx@qq.com',//你的郵箱
    // 這里密碼不是qq密碼,是你設置的smtp授權碼
    pass: 'qq的授權碼',
  }
});

let mailOptions = {
  from: '"火星黑洞" <1418275530@qq.com>', // sender address
  to: '156093340@qq.com', // list of receivers
  subject: 'Hello', // Subject line
  // 發送text或者html格式
  // text: 'Hello 我是火星黑洞', // plain text body
  html: '<b>Hello 我是火星黑洞</b>' // html body
};

// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
  if (error) {
    return console.log(error);
  }
  console.log('Message sent: %s', info.messageId);
  // Message sent: <04ec7731-cc68-1ef6-303c-61b0f796b78f@qq.com>
});

第三步,執行 node email.js,即可發送成功~

 

注:可以前往 https://jingyan.baidu.com/article/fedf0737af2b4035ac8977ea.html 去查看如何設置授權碼

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM