node.js 發郵件

安裝依賴nodemailer:npm install nodemailer -s

配置發件終端:

let senderMsg = nodemailer.createTransport({
  host: 'smtp.qq.com', // qq郵箱的服務器
  port: 465, // qq郵箱對應的端口
  secureConnection: true, // 不寫這句會報錯:Greeting never received
  auth: {
    user: '[email protected]', // 發件人的郵箱賬號
    pass: '*************' // 郵箱的授權碼
  }
})

填寫發件信息:

let receiverMsg = {
  from: '[email protected]', // 發件人郵箱
  to: '[email protected]', // 收件人郵箱
  subject: '測試發郵件', // 主題
  text: '你能不能收到我發的郵件呢', // 普通文本格式的郵件內容
  html: '', // html格式的郵件內容
  attachments: [] // 附件
}

發送行爲:

let postMail = function () {
  senderMsg.sendMail(receiverMsg,(error,info)=>{
    if(error) {
      console.log(error);
    } else {
      console.log(`Message: ${info.messageId}`);
      console.log(`sent: ${info.response}`);
    }
  });
}

QQ郵箱獲取授權碼方法:

https://blog.csdn.net/qq_39818325/article/details/82890954

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章