最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

node.js 基于 STMP 協(xié)議和 EWS 協(xié)議發(fā)送郵件

 更新時(shí)間:2021年02月14日 15:17:23   作者:志文工作室  
這篇文章主要介紹了node.js 基于 STMP 協(xié)議和 EWS 協(xié)議發(fā)送郵件的示例,幫助大家更好的理解和使用node.js,感興趣的朋友可以了解下

本文主要介紹 node.js 發(fā)送基于 STMP 協(xié)議和 MS Exchange Web Service(EWS) 協(xié)議的郵件的方法。文中所有參考代碼均以 TypeScript 編碼示例。

1 基于 STMP 協(xié)議的 node.js 發(fā)送郵件方法

提到使用 node.js 發(fā)送郵件,基本都會(huì)提到大名鼎鼎的 Nodemailer 模塊,它是當(dāng)前使用 STMP 方式發(fā)送郵件的首選。
基于 NodeMailer 發(fā)送 STMP 協(xié)議郵件的文章網(wǎng)上已非常多,官方文檔介紹也比較詳細(xì),在此僅列舉示例代碼以供對(duì)比參考:
封裝一個(gè) sendMail 郵件發(fā)送方法:

/**
 * 使用 Nodemailer 發(fā)送 STMP 郵件
 * @param {Object} opts 郵件發(fā)送配置
 * @param {Object} smtpCfg smtp 服務(wù)器配置
 */
async function sendMail(opts, smtpCfg) {
 const resultInfo = { code: 0, msg: '', result: null };
 if (!smtpCfg) {
 resultInfo.msg = '未配置郵件發(fā)送信息';
 resultInfo.code = - 1009;
 return resultInfo;
 }

 // 創(chuàng)建一個(gè)郵件對(duì)象
 const mailOpts = Object.assign(
 {
 // 發(fā)件人
 from: `Notify <${smtpCfg.auth.user}>`,
 // 主題
 subject: 'Notify',
 // text: opts.content,
 // html: opts.content,
 // 附件內(nèi)容
 // /*attachments: [{
 // filename: 'data1.json',
 // path: path.resolve(__dirname, 'data1.json')
 // }, {
 // filename: 'pic01.jpg',
 // path: path.resolve(__dirname, 'pic01.jpg')
 // }, {
 // filename: 'test.txt',
 // path: path.resolve(__dirname, 'test.txt')
 // }],*/
 },
 opts
 );

 if (!mailOpts.to) mailOpts.to = [];
 if (!Array.isArray(mailOpts.to)) mailOpts.to = String(mailOpts.to).split(',');
 mailOpts.to = mailOpts.to.map(m => String(m).trim()).filter(m => m.includes('@'));

 if (!mailOpts.to.length) {
 resultInfo.msg = '未配置郵件接收者';
 resultInfo.code = - 1010;
 return resultInfo;
 }

 const mailToList = mailOpts.to;
 const transporter = nodemailer.createTransport(smtpCfg);

 // to 列表分開發(fā)送
 for (const to of mailToList) {
 mailOpts.to = to.trim();
 try {
 const info = await transporter.sendMail(mailOpts);
 console.log('mail sent to:', mailOpts.to, ' response:', info.response);
 resultInfo.msg = info.response;
 } catch (error) {
 console.log(error);
 resultInfo.code = -1001;
 resultInfo.msg = error;
 }
 }

 return resultInfo;
}

使用 sendMail 方法發(fā)送郵件:

const opts = {
 subject: 'subject for test',
 /** HTML 格式郵件正文內(nèi)容 */
 html: `email content for test: <a  rel="external nofollow" rel="external nofollow" >https://lzw.me</a>`,
 /** TEXT 文本格式郵件正文內(nèi)容 */
 text: '',
 to: 'xxx@lzw.me',
 // 附件列表
 // attachments: [],
};
const smtpConfig = {
 host: 'smtp.qq.com', //QQ: smtp.qq.com; 網(wǎng)易: smtp.163.com
 port: 465, //端口號(hào)。QQ郵箱 465,網(wǎng)易郵箱 25
 secure: true,
 auth: {
 user: 'xxx@qq.com', //郵箱賬號(hào)
 pass: '', //郵箱的授權(quán)碼
 },
};
sendMail(opts, smtpConfig).then(result => console.log(result));

2 基于 MS Exchange 郵件服務(wù)器的 node.js 發(fā)送郵件方法

對(duì)于使用微軟的 Microsoft Exchange Server 搭建的郵件服務(wù),Nodemailer 就無能為力了。Exchange Web Service(EWS)提供了訪問 Exchange 資源的接口,在微軟官方文檔中對(duì)其有詳細(xì)的接口定義文檔。針對(duì) Exchange 郵件服務(wù)的流行第三方庫主要有 node-ews 和 ews-javascript-api。

2.1 使用 node-ews 發(fā)送 MS Exchange 郵件

下面以 node-ews 模塊為例,介紹使用 Exchange 郵件服務(wù)發(fā)送郵件的方法。

2.1.1 封裝一個(gè)基于 node-ews 發(fā)送郵件的方法

封裝一個(gè) sendMailByNodeEws 方法:

import EWS from 'node-ews';

export interface IEwsSendOptions {
 auth: {
 user: string;
 pass?: string;
 /** 密碼加密后的秘鑰(NTLMAuth.nt_password)。為字符串時(shí),應(yīng)為 hex 編碼結(jié)果 */
 nt_password?: string | Buffer;
 /** 密碼加密后的秘鑰(NTLMAuth.lm_password)。為字符串時(shí),應(yīng)為 hex 編碼結(jié)果 */
 lm_password?: string | Buffer;
 };
 /** Exchange 地址 */
 host?: string;
 /** 郵件主題 */
 subject?: string;
 /** HTML 格式郵件正文內(nèi)容 */
 html?: string;
 /** TEXT 文本格式郵件正文內(nèi)容(優(yōu)先級(jí)低于 html 參數(shù)) */
 text?: string;
 to?: string;
}

/**
 * 使用 Exchange(EWS) 發(fā)送郵件
 */
export async function sendMailByNodeEws(options: IEwsSendOptions) {
 const resultInfo = { code: 0, msg: '', result: null };

 if (!options) {
 resultInfo.code = -1001;
 resultInfo.msg = 'Options can not be null';
 } else if (!options.auth) {
 resultInfo.code = -1002;
 resultInfo.msg = 'Options.auth{user,pass} can not be null';
 } else if (!options.auth.user || (!options.auth.pass && !options.auth.lm_password)) {
 resultInfo.code = -1003;
 resultInfo.msg = 'Options.auth.user or Options.auth.password can not be null';
 }

 if (resultInfo.code) return resultInfo;

 const ewsConfig = {
 username: options.auth.user,
 password: options.auth.pass,
 nt_password: options.auth.nt_password,
 lm_password: options.auth.lm_password,
 host: options.host,
 // auth: 'basic',
 };

 if (ewsConfig.nt_password && typeof ewsConfig.nt_password === 'string') {
 ewsConfig.nt_password = Buffer.from(ewsConfig.nt_password, 'hex');
 }

 if (ewsConfig.lm_password && typeof ewsConfig.lm_password === 'string') {
 ewsConfig.lm_password = Buffer.from(ewsConfig.lm_password, 'hex');
 }

 Object.keys(ewsConfig).forEach(key => {
 if (!ewsConfig[key]) delete ewsConfig[key];
 });

 // initialize node-ews
 const ews = new EWS(ewsConfig);
 // define ews api function
 const ewsFunction = 'CreateItem';
 // define ews api function args
 const ewsArgs = {
 attributes: {
  MessageDisposition: 'SendAndSaveCopy',
 },
 SavedItemFolderId: {
  DistinguishedFolderId: {
  attributes: {
   Id: 'sentitems',
  },
  },
 },
 Items: {
  Message: {
  ItemClass: 'IPM.Note',
  Subject: options.subject,
  Body: {
   attributes: {
   BodyType: options.html ? 'HTML' : 'Text',
   },
   $value: options.html || options.text,
  },
  ToRecipients: {
   Mailbox: {
   EmailAddress: options.to,
   },
  },
  IsRead: 'false',
  },
 },
 };

 try {
 const result = await ews.run(ewsFunction, ewsArgs);
 // console.log('mail sent to:', options.to, ' response:', result);
 resultInfo.result = result;
 if (result.ResponseMessages.MessageText) resultInfo.msg = result.ResponseMessages.MessageText;
 } catch (err) {
 console.log(err.stack);
 resultInfo.code = 1001;
 resultInfo.msg = err.stack;
 }

 return resultInfo;
}

使用 sendMailByNodeEws 方法發(fā)送郵件:

sendMailByNodeEws({
 auth: {
 user: 'abc@xxx.com',
 pass: '123456',
 /** 密碼加密后的秘鑰(NTLMAuth.nt_password)。為字符串時(shí),應(yīng)為 hex 編碼結(jié)果 */
 nt_password: '',
 /** 密碼加密后的秘鑰(NTLMAuth.lm_password)。為字符串時(shí),應(yīng)為 hex 編碼結(jié)果 */
 lm_password: '',
 },
 /** Exchange 地址 */
 host: 'https://ews.xxx.com',
 /** 郵件主題 */
 subject: 'subject for test',
 /** HTML 格式郵件正文內(nèi)容 */
 html: `email content for test: <a  rel="external nofollow" rel="external nofollow" >https://lzw.me</a>`,
 /** TEXT 文本格式郵件正文內(nèi)容(優(yōu)先級(jí)低于 html 參數(shù)) */
 text: '',
 to: 'xxx@lzw.me',
})

2.1.2 基于 NTLMAuth 的認(rèn)證配置方式

直接配置 pass 密碼可能會(huì)導(dǎo)致明文密碼泄露,我們可以將 pass 字段留空,配置 nt_password 和 lm_password 字段,使用 NTLMAuth 認(rèn)證模式。此二字段基于 pass 明文生成,其 nodejs 生成方式可借助 httpntlm 模塊完成,具體參考如下:

import { ntlm as NTLMAuth } from 'httpntlm';

/** 將輸入的郵箱賬號(hào)密碼轉(zhuǎn)換為 NTLMAuth 秘鑰(hex)格式并輸出 */
const getHashedPwd = () => {
 const passwordPlainText = process.argv.slice(2)[0];

 if (!passwordPlainText) {
 console.log('USEAGE: \n\tnode get-hashed-pwd.js [password]');
 return;
 }

 const nt_password = NTLMAuth.create_NT_hashed_password(passwordPlainText.trim());
 const lm_password = NTLMAuth.create_LM_hashed_password(passwordPlainText.trim());

 // console.log('\n password:', passwordPlainText);
 console.log(` nt_password:`, nt_password.toString('hex'));
 console.log(` lm_password:`, lm_password.toString('hex'));

 return {
 nt_password,
 lm_password,
 };
};

getHashedPwd();

2.2 使用 ews-javascript-api 發(fā)送 MS Exchange 郵件

基于 ews-javascript-api 發(fā)送郵件的方式,在其官方 wiki 有相關(guān)示例,但本人在測(cè)試過程中未能成功,具體為無法取得服務(wù)器認(rèn)證,也未能查證具體原因,故以下代碼僅作參考:

/**
 * 使用 `ews-javascript-api` 發(fā)送(MS Exchange)郵件
 */
export async function sendMailByEwsJApi(options: IEwsSendOptions) {
 const resultInfo = { code: 0, msg: '', result: null };

 if (!options) {
 resultInfo.code = -1001;
 resultInfo.msg = 'Options can not be null';
 } else if (!options.auth) {
 resultInfo.code = -1002;
 resultInfo.msg = 'Options.auth{user,pass} can not be null';
 } else if (!options.auth.user || (!options.auth.pass && !options.auth.lm_password)) {
 resultInfo.code = -1003;
 resultInfo.msg = 'Options.auth.user or Options.auth.password can not be null';
 }

 const ews = require('ews-javascript-api');
 const exch = new ews.ExchangeService(ews.ExchangeVersion.Exchange2010);
 exch.Credentials = new ews.WebCredentials(options.auth.user, options.auth.pass);
 exch.Url = new ews.Uri(options.host);
 ews.EwsLogging.DebugLogEnabled = true; // false to turnoff debugging.

 const msgattach = new ews.EmailMessage(exch);
 msgattach.Subject = options.subject;
 msgattach.Body = new ews.MessageBody(ews.BodyType.HTML, escape(options.html || options.text));
 if (!Array.isArray(options.to)) options.to = [options.to];
 options.to.forEach(to => msgattach.ToRecipients.Add(to));
 // msgattach.Importance = ews.Importance.High;

 // 發(fā)送附件
 // msgattach.Attachments.AddFileAttachment('filename to attach.txt', 'c29tZSB0ZXh0');

 try {
 const result = await msgattach.SendAndSaveCopy(); // .Send();
 console.log('DONE!', result);
 resultInfo.result = result;
 } catch (err) {
 console.log('ERROR:', err);
 resultInfo.code = 1001;
 resultInfo.msg = err;
 }
 return resultInfo;
}

3 擴(kuò)展參考

nodemailer.com/about/
github.com/CumberlandG…
github.com/gautamsi/ew…
github.com/lzwme/node-…

以上就是node.js 基于 STMP 協(xié)議和 EWS 協(xié)議發(fā)送郵件的詳細(xì)內(nèi)容,更多關(guān)于node.js 發(fā)送郵件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

武夷山市| 东港市| 枣庄市| 阳泉市| 黔西县| 钟山县| 金山区| 甘孜| 会同县| 泰来县| 镇原县| 杭锦后旗| 山阴县| 大余县| 泸州市| 本溪市| 晋城| 吉林市| 太保市| 洛阳市| 洛川县| 苏尼特右旗| 桓仁| 哈尔滨市| 大悟县| 合肥市| 阿坝县| 田东县| 沂南县| 黄山市| 剑阁县| 石楼县| 兰州市| 古蔺县| 深水埗区| 堆龙德庆县| 山东| 林芝县| 安仁县| 抚远县| 蕲春县|