python - 如何在 Outlook (2010) 全局地址列表中搜索多个姓名?

标签 python python-3.x outlook gal

我读过这篇文章How can I search the Outlook (2010) Global Address List for a name?并找到了从 Outlook GAL 获取名称的有效解决方案。

我有 3 个问题:

  1. 如果 search_string 是电子邮件地址,我就可以获得联系信息。当它是一个名字时,搜索就不起作用。它将返回 False 表示已解决,但返回 True 表示可发送。然后使用 ae 对象时出现错误。我做错了什么?

  2. 我不太了解代码,无法修改它来搜索多个名称。我只是创建了一个 for 循环,但也许有更有效的方法?例如,我可以在不同搜索之间重用 outlook.Session 对象吗?

  3. recipient.Resolve() 行是否必要?

提前致谢!

我的尝试如下。

from __future__ import print_function
import win32com.client

search_strings = ['Doe John', 'Doe Jane']

outlook = win32com.client.gencache.EnsureDispatch('Outlook.Application')

for search_string in search_strings:
    recipient = outlook.Session.CreateRecipient(search_string)
    recipient.Resolve()
    print('Resolved OK: ', recipient.Resolved)
    print('Is it a sendable? (address): ', recipient.Sendable)
    print('Name: ', recipient.Name)

    ae = recipient.AddressEntry
    email_address = None

    if 'EX' == ae.Type:
        eu = ae.GetExchangeUser()
        email_address = eu.PrimarySmtpAddress

    if 'SMTP' == ae.Type:
        email_address = ae.Address

    print('Email address: ', email_address)

最佳答案

不敢相信我在发布问题后这么快就找到了解决方案。因为很难找到答案。我在这里分享我的发现。

它的灵感来自 How to fetch exact match of addressEntry object from GAL (Global Address List) ,尽管它是用 C# 而不是 Python 编写的。

此方法使用显示名称的精确匹配,而不是依赖 Outlook 来解​​析名称。不过,可以循环遍历全局地址列表并自己进行部分匹配。

import win32com.client

search_string = 'Doe John'

outlook = win32com.client.gencache.EnsureDispatch('Outlook.Application')
gal = outlook.Session.GetGlobalAddressList()
entries = gal.AddressEntries
ae = entries[search_string]
email_address = None

if 'EX' == ae.Type:
    eu = ae.GetExchangeUser()
    email_address = eu.PrimarySmtpAddress

if 'SMTP' == ae.Type:
    email_address = ae.Address

print('Email address: ', email_address)

关于python - 如何在 Outlook (2010) 全局地址列表中搜索多个姓名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54795036/

相关文章:

python - 如何防止 wx 面板与其他 sizer 重叠

css - ".msg"字体颜色在 Mac 和 Windows 上不同

html - Gmail 在电子邮件签名中的空 td 中添加了高度

python - 如何搜索音频文件中的内容?

python - Python 性能算法中的不均匀共享

python-3.x - 收集特殊键和值并将其放在字典中

python - 查找两个Python对象之间的差异

python - 列表推导比 for 循环有什么优势?

python - 在python中将SQL表作为JSON返回

php - 如何将投票按钮限制为 'To' 地址而不是通过 powershell 在 outlook 中发送到 'Cc'?