node.js發送郵件

步驟一,開啓郵箱服務

在郵箱設置裏面開啓POP3/SMTP/IMAP服務,如圖html

clipboard.png

步驟二,安裝nodemailer插件

npm install -save nodemailer

步驟三,構造以下代碼

const nodemailer=require('nodemailer');
let transporter=nodemailer.createTransport({
    host:'smtp.126.com',
    secure:true,
    auth:{
        user:'*****@126.com',
        pass:'*****'

    },
});

let mailOptions={
    from:'******@126.com',
    to:'******@163.com',
    subject:'天氣',
    text:'今每天氣很好!',
    html:'<b>今每天氣很好!</b>'
};

transporter.sendMail(mailOptions,(err,info)=>{
    if(err){
        console.log(err);
    }
    else{
        console.log(info);
    }
});

步驟四,測試

clipboard.png

注意點

發送程序中,subject,和主題須要相一致,否則致使以下錯誤,504,SUM 信封發件人和信頭髮件人不匹配node

{ Error: Message failed: 554 DT:SPM 126 smtp7,DsmowAA3nqryKy1Zc0oRJw--.15930S2 1496132596,please see http://mail.163.com/help/help_spam_16.htm?ip=111.74.215.148&hostid=smtp7&time=1496132596
    at SMTPConnection._formatError (D:\node\likeread\node_modules\nodemailer\lib\smtp-connection\index.js:557:19)
    at SMTPConnection._actionSMTPStream (D:\node\likeread\node_modules\nodemailer\lib\smtp-connection\index.js:1385:34)
    at SMTPConnection._responseActions.push.str (D:\node\likeread\node_modules\nodemailer\lib\smtp-connection\index.js:907:22)
    at SMTPConnection._processResponse (D:\node\likeread\node_modules\nodemailer\lib\smtp-connection\index.js:706:20)
    at SMTPConnection._onData (D:\node\likeread\node_modules\nodemailer\lib\smtp-connection\index.js:509:14)
    at TLSSocket._socket.on.chunk (D:\node\likeread\node_modules\nodemailer\lib\smtp-connection\index.js:461:47)
    at emitOne (events.js:96:13)
    at TLSSocket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at TLSSocket.Readable.push (_stream_readable.js:134:10)
  code: 'EMESSAGE',
  response: '554 DT:SPM 126 smtp7,DsmowAA3nqryKy1Zc0oRJw--.15930S2 1496132596,please see http://mail.163.com/help/help_spam_16.htm?ip=111.74.215.148&hostid=smtp7&time=1496132596',
  responseCode: 554,
  command: 'DATA' }
相關文章
相關標籤/搜索