Outlook加载项开发:如何读取联系人组中的收件人列表?
最优方案:使用EWS的ExpandDl操作
你说得没错,Office.js的Recipients接口确实没有提供直接展开通讯组(distributionList)成员的能力,目前最优且官方支持的方案就是通过EWS的ExpandDl操作来实现,以下是具体的实现步骤和注意事项:
核心实现步骤
提取通讯组的SMTP地址
遍历邮件的收件人列表(to/cc/bcc),筛选出类型为distributionList的收件人,获取其emailAddress属性值:// 获取撰写邮件的收件人列表 const recipients = Office.context.mailbox.item.to.getAsync((result) => { if (result.status === Office.AsyncResultStatus.Succeeded) { const distributionLists = result.value.filter(recipient => recipient.type === Office.MailboxEnums.RecipientType.DistributionList); // 遍历每个通讯组,获取SMTP地址 distributionLists.forEach(dl => { const dlAddress = dl.emailAddress; // 调用ExpandDl操作 expandDistributionList(dlAddress); }); } });构造EWS的ExpandDl请求
生成符合EWS规范的SOAP请求体,传入通讯组的SMTP地址:<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Header> <t:RequestServerVersion Version="Exchange2013" /> </soap:Header> <soap:Body> <m:ExpandDL> <m:Mailbox> <t:EmailAddress>{DL_SMTP_ADDRESS}</t:EmailAddress> </m:Mailbox> </m:ExpandDL> </soap:Body> </soap:Envelope>注意替换
{DL_SMTP_ADDRESS}为实际的通讯组邮箱地址。通过Office.js发送EWS请求
使用Office.context.mailbox.makeEwsRequestAsync方法发送构造好的SOAP请求,并处理响应:function expandDistributionList(dlAddress) { const soapRequest = `<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Header> <t:RequestServerVersion Version="Exchange2013" /> </soap:Header> <soap:Body> <m:ExpandDL> <m:Mailbox> <t:EmailAddress>${dlAddress}</t:EmailAddress> </m:Mailbox> </m:ExpandDL> </soap:Body> </soap:Envelope>`; Office.context.mailbox.makeEwsRequestAsync(soapRequest, (result) => { if (result.status === Office.AsyncResultStatus.Succeeded) { // 解析SOAP响应,提取成员信息 const members = parseExpandDlResponse(result.value); console.log("通讯组成员:", members); // 后续处理成员列表 } else { console.error("展开通讯组失败:", result.error); } }); }解析EWS响应
从返回的SOAP XML中提取成员的邮箱地址和显示名称,示例解析逻辑:function parseExpandDlResponse(xmlResponse) { const parser = new DOMParser(); const xmlDoc = parser.parseFromString(xmlResponse, "text/xml"); const memberNodes = xmlDoc.querySelectorAll("t:Mailbox"); const members = []; memberNodes.forEach(node => { const emailAddress = node.querySelector("t:EmailAddress")?.textContent; const displayName = node.querySelector("t:Name")?.textContent; if (emailAddress) { members.push({ displayName, emailAddress }); } }); return members; }
关键注意事项
- 权限配置:在加载项的manifest文件中,必须配置足够的权限才能调用EWS操作,建议设置为:
<Permissions>ReadWriteMailbox</Permissions> - 嵌套通讯组处理:默认ExpandDl只展开一级成员,如果通讯组包含子组,需要递归调用ExpandDl来展开所有层级。
- 性能与兼容性:对于大型通讯组,请求响应时间可能较长,建议添加加载状态提示;同时确保目标Exchange服务器版本支持ExpandDl操作(Exchange 2010及以上版本均支持)。
- 撰写模式兼容性:
makeEwsRequestAsync在撰写模式和阅读模式下均可用,但需确保Office.context.mailbox.item已正确加载。
内容的提问来源于stack exchange,提问作者Matthew Hein
相关产品推荐
相关产品推荐

