You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python Pywin32获取Outlook Exchange用户所属邮件通讯组的方法

实现方案

要提取Exchange用户所属的邮件组名称,可直接调用地址条目对象的GetMemberOfList()方法实现,修改后的完整代码如下:

import win32com.client
import pandas as pd 
# Outlook stuff
outApp = win32com.client.gencache.EnsureDispatch("Outlook.Application")
outGAL = outApp.Session.GetGlobalAddressList()
entries = outGAL.AddressEntries
data_set = list()

# 遍历通讯录提取用户信息
for entry in entries:
    if entry.Type == "EX":
        user = entry.GetExchangeUser()
        if user is not None:
            if len(user.FirstName) > 0 or len(user.LastName) > 0:
                row = list()
                row.append(user.FirstName)
                row.append(user.LastName)
                row.append(user.PrimarySmtpAddress)
                row.append(user.Department)
                row.append(user.BusinessTelephoneNumber)
                row.append(user.MobileTelephoneNumber)
                row.append(user.CompanyName)
                row.append(user.Name)
                row.append(user.JobTitle)
                row.append(user.OfficeLocation)
                row.append(user.Alias)
                row.append(user.City)
                row.append(user.Comments)
                row.append(user.StateOrProvince)
                row.append(user.StreetAddress)
                # 新增:提取所属邮件组名称
                group_names = []
                try:
                    group_list = entry.GetMemberOfList()
                    for group in group_list:
                        # 可选筛选:仅保留邮件通讯组,过滤其他类型的组
                        # if group.AddressEntryUserType == 1: # olExchangeDistributionList 枚举值为1
                        group_names.append(group.Name)
                except Exception as e:
                    # 权限不足或无对应组时捕获异常,避免程序中断
                    pass
                row.append(",".join(group_names))
                data_set.append(row)

注意事项:

  • 该方法仅能拉取全局地址列表中公开可见的通讯组,隐藏的私有组/受限组不会返回
  • 遍历所有用户拉取组会显著增加执行耗时,用户量较大时建议按需添加缓存逻辑
  • 部分受限的Exchange账户可能没有调用该接口的权限,代码中已添加异常捕获避免中断

内容的提问来源于stack exchange,提问作者Husnain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 04:45:10