如何在Outlook(2010)全球地址列表中搜索多个名称?

时间:2019-02-20 20:50:49

标签: 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)

1 个答案:

答案 0 :(得分:0)

简直无法相信我在发布问题后很快找到了解决方案。由于很难找到答案。我在这里分享我的发现。

它受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)