1. 程式人生 > >node.js實現傳送郵件功能

node.js實現傳送郵件功能

準備事項

  1. QQ郵箱設定:進入QQ郵箱->設定->賬戶->POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服務->開啟POP3/SMTP服務,並複製pass祕鑰
  2. 安裝nodemailer :npm install nodemailer --save
  3. 基於express的node.js後臺

程式碼編寫

router.post('/sendEmails', (req,res,next)=>{
    var transport = nodemailer.createTransport({
        host: "smtp.qq.com",
        secureConnection: true,
        port: 465,
        auth: {
            user: "
[email protected]
", pass: "xxxxxxx" //剛才 } }) var mailOptions = { from: "[email protected]", to: "[email protected]", subject: "一封郵件", text: "hello hello" } transport.sendMail(mailOptions,function(err,response){ if(err) console.log(err) else console.log(response) }) })