国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JavaScript > 正文

node.js發(fā)送郵件email的方法詳解

2019-11-19 18:06:39
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了node.js發(fā)送郵件email的方法。分享給大家供大家參考,具體如下:

通常我們做node項(xiàng)目時(shí),可能我們會(huì)碰到做一個(gè)簡(jiǎn)單的郵件反饋,那么我們今天就來(lái)討論一下,其中遇到的各種坑。

總的來(lái)說(shuō)做這個(gè)東西,我們可能需要node第三方依賴模塊,來(lái)實(shí)現(xiàn)我們要達(dá)到的效果。

這里我推薦兩個(gè)模塊:https://github.com/pingfanren/Nodemailer

npm install nodemailer//這個(gè)模塊不錯(cuò),github上星也比較多,還經(jīng)常有維護(hù),但是坑也比較多

另一個(gè),https://github.com/eleith/emailjs

npm install emailjs --save

這里我用的是nodemailer模塊,畢竟用的人比較多,跟隨主流呢

它的特點(diǎn):

使用Unicode編碼
支持Windows系統(tǒng),不需要安裝依賴
支持純文本和HTML格式
支持發(fā)送附件(包括大型附件)
在HTML中嵌入圖片
支持SSL/STARTTLS安全協(xié)議
不同的傳輸方法,可以使用內(nèi)置也可以使用外部插件的形式
提供自定義插件支持(比如增加DKIM簽名,使用markdown代替HTML等等)
支持XOAUTH2登錄驗(yàn)證(以及關(guān)于更新的令牌反饋)

安裝使用

npm install nodemailer --save

使用內(nèi)置傳輸發(fā)送郵件,可以查看支持列表:https://github.com/andris9/nodemailer-wellknown#supported-services

var nodemailer = require('nodemailer');var transporter = nodemailer.createTransport({  //https://github.com/andris9/nodemailer-wellknown#supported-services 支持列表  service: 'qq',  port: 465, // SMTP 端口  secureConnection: true, // 使用 SSL  auth: {    user: '768065158@qq.com',    //這里密碼不是qq密碼,是你設(shè)置的smtp密碼    pass: '*****'  }});// NB! No need to recreate the transporter object. You can use// the same transporter object for all e-mails// setup e-mail data with unicode symbolsvar mailOptions = {  from: '768065158@qq.com', // 發(fā)件地址  to: '528779822@qq.com', // 收件列表  subject: 'Hello sir', // 標(biāo)題  //text和html兩者只支持一種  text: 'Hello world ?', // 標(biāo)題  html: '<b>Hello world ?</b>' // html 內(nèi)容};// send mail with defined transport objecttransporter.sendMail(mailOptions, function(error, info){  if(error){    return console.log(error);  }  console.log('Message sent: ' + info.response);});

發(fā)送郵件成功以后我們很少會(huì)有操作,但也有極少數(shù)情況需要在成功以后會(huì)處理一些特殊信息的,這時(shí)候info對(duì)象就能發(fā)揮余熱了。info對(duì)象中包含了messageId、envelop、accepted和response等屬性,具體看文檔我不一一介紹了。

使用其他傳輸插件   https://github.com/andris9/nodemailer-smtp-transport

npm install nodemailer-smtp-transport --save

其他代碼類似,差別只是在創(chuàng)建transport上,所以這里我就寫(xiě)一部分代碼:

var nodemailer = require('nodemailer');var smtpTransport = require('nodemailer-smtp-transport');// 開(kāi)啟一個(gè) SMTP 連接池var transport = nodemailer.createTransport(smtpTransport({ host: "smtp.qq.com", // 主機(jī) secure: true, // 使用 SSL secureConnection: true, // 使用 SSL port: 465, // SMTP 端口 auth: {  user: "gaolu19901228@qq.com", // 賬號(hào)  pass: "******" // 密碼 }}));// 設(shè)置郵件內(nèi)容var mailOptions = { from: "768065158<768065158@qq.com>", // 發(fā)件地址 to: "528779822@qq.com", // 收件列表 subject: "Hello world", // 標(biāo)題 text:"hello", html: "<b>thanks a for visiting!</b> 世界,你好!" // html 內(nèi)容}// 發(fā)送郵件transport.sendMail(mailOptions, function(error, response) { if (error) {  console.error(error); } else {  console.log(response); } transport.close(); // 如果沒(méi)用,關(guān)閉連接池});

下面列出了一些發(fā)郵件的字段:

from 發(fā)送者郵箱
sender 發(fā)送者區(qū)域顯示的信息
to 接收者郵箱
cc 抄送者郵箱
bcc 密送者郵箱
subject 郵箱主題
attachments 附件內(nèi)容
watchHtml apple watch指定的html版本
text 文本信息
html html內(nèi)容
headers 另加頭信息
encoding 編碼格式

郵件內(nèi)容使用UTF-8格式,附件使用二進(jìn)制流。

附件

附件對(duì)象包含了下面這些屬性:

filename 附件名
content 內(nèi)容
encoding 編碼格式
path 文件路徑
contentType 附件內(nèi)容類型

常見(jiàn)錯(cuò)誤

1.賬號(hào)未設(shè)置該服務(wù)

{ [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郵箱 -> 設(shè)置 -> 帳戶 -> 開(kāi)啟服務(wù):POP3/SMTP服務(wù)

2.發(fā)件賬號(hào)與認(rèn)證賬號(hào)不同

{ [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' }

3.登錄認(rèn)證失敗,可能由于smpt獨(dú)立密碼錯(cuò)誤導(dǎo)致 我在qq設(shè)置的時(shí)候就遇到過(guò)

Invalid login - 535 Authentication failed

解決方案:

qq郵箱在測(cè)試smtp郵件服務(wù)器時(shí),一,在qq郵箱,設(shè)置,賬戶設(shè)置中.開(kāi)啟下smtp.二,設(shè)置一下獨(dú)立密碼.三,在配置smtp服務(wù)器的密碼時(shí),注意一定要填你設(shè)置的獨(dú)立密碼.不要用郵箱登錄密碼.否則會(huì)提示535 Authentication failed錯(cuò)誤.

希望本文所述對(duì)大家nodejs程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 临夏县| 南漳县| 璧山县| 道真| 航空| 萨迦县| 江永县| 南溪县| 老河口市| 威宁| 巴彦淖尔市| 微山县| 沙田区| 高唐县| 仪征市| 光泽县| 贵州省| 河源市| 罗城| 界首市| 吴旗县| 昭平县| 临海市| 右玉县| 宝兴县| 文化| 上饶市| 紫金县| 湄潭县| 衡水市| 苍南县| 商水县| 大名县| 疏附县| 黄山市| 大厂| 信丰县| 清苑县| 玉树县| 禹城市| 汉源县|