node.js使用nodemailer發送郵件執行個體

來源:互聯網
上載者:User

一、安裝 nodemailer
複製代碼 代碼如下:npm install nodemailer --save
二、調用
複製代碼 代碼如下:var nodemailer = require("nodemailer");

// 開啟一個 SMTP 串連池
var smtpTransport = nodemailer.createTransport("SMTP",{
  host: "smtp.qq.com", // 主機
  secureConnection: true, // 使用 SSL
  port: 465, // SMTP 連接埠
  auth: {
    user: "xxxxxxxx@qq.com", // 帳號
    pass: "xxxxxxxx" // 密碼
  }
});

// 設定郵件內容
var mailOptions = {
  from: "Fred Foo <xxxxxxxx@qq.com>", // 發件地址
  to: "2838890xx@qq.com, minimixx@126.com", // 收件列表
  subject: "Hello world", // 標題
  html: "<b>thanks a for visiting!</b> 世界,你好!" // html 內容
}

// 發送郵件
smtpTransport.sendMail(mailOptions, function(error, response){
  if(error){
    console.log(error);
  }else{
    console.log("Message sent: " + response.message);
  }
  smtpTransport.close(); // 如果沒用,關閉串連池
});
三、常見錯誤
複製代碼 代碼如下:
{ [AuthError: Invalid login - 454 Authentication failed, please open smtp flag first!]
  name: 'AuthError',
  data: '454 Authentication failed, please open smtp flag first!',
  stage: 'auth' }

錯誤原因: 帳號未設定該服務
解決方案: QQ郵箱 -> 設定 -> 帳戶 -> 開啟服務:POP3/SMTP服務
複製代碼 代碼如下:
{ [SenderError: Mail from command failed - 501 mail from address must be same as authorization user]
  name: 'SenderError',
  data: '501 mail from address must be same as authorization user',
  stage: 'mail' }
錯誤原因: 發件帳號與認證帳號不同

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.