如何在 Outlook (2010) 全局地址列表中搜索名称?
How can I search the Outlook (2010) Global Address List for a name?
我有一份名单,其中一些是完整的,一些是运行分类的。我想在 Outlook 地址列表中搜索这些名称的匹配项。
我最接近的是这个 Python 代码 which came from ActiveState Code,但它不搜索全局地址,只搜索我的(本地?)列表,其中有 3 个地址,这是显然不对。应该有几千条记录。
欢迎任何提示。我用谷歌搜索并阅读了几十页,但没有定论。我宁愿不直接连接到 LDAP,我认为这在我的组织中是违反政策的,而且我不确定这是否可能。如果可能,希望通过 Outlook API 执行此操作。
DEBUG=1
class MSOutlook:
def __init__(self):
self.outlookFound = 0
try:
self.oOutlookApp = \
win32com.client.gencache.EnsureDispatch("Outlook.Application")
self.outlookFound = 1
except:
print("MSOutlook: unable to load Outlook")
self.records = []
def loadContacts(self, keys=None):
if not self.outlookFound:
return
# this should use more try/except blocks or nested blocks
onMAPI = self.oOutlookApp.GetNamespace("MAPI")
ofContacts = \
onMAPI.GetDefaultFolder(win32com.client.constants.olFolderContacts)
if DEBUG:
print("number of contacts:", len(ofContacts.Items))
for oc in range(len(ofContacts.Items)):
contact = ofContacts.Items.Item(oc + 1)
if contact.Class == win32com.client.constants.olContact:
if keys is None:
# if we were't give a set of keys to use
# then build up a list of keys that we will be
# able to process
# I didn't include fields of type time, though
# those could probably be interpreted
keys = []
for key in contact._prop_map_get_:
if isinstance(getattr(contact, key), (int, str, unicode)):
keys.append(key)
if DEBUG:
keys.sort()
print("Fields\n======================================")
for key in keys:
print(key)
record = {}
for key in keys:
record[key] = getattr(contact, key)
if DEBUG:
print(oc, record['FullName'])
self.records.append(record)
随机links:
MSDN - How to create a Global Address List programmatically using Visual Basic
Infinitec - How to get the Global Address List programatically
Return Boolean True - Downloading the Global Address List from Outlook/Exchange
Grokbase - [python-win32] getting global addressbook with extended mapi
Stack Overflow - Searching Outlook Global Address
再次尝试列表失败
Stack Overflow - Retrieving outlook Contacts via python
这是我得到上面link的地方
Stack Overflow - Fetching Outlook Contacts with Python
根本运行
Python for Windows - Automating Microsoft Outlook
只是默认地址簿。另外我要搜索,不一一列举。
如果有人能提出解决方案,我不介意它是 C++、VB、Perl、Python 等
您上面的代码处理默认联系人文件夹中的联系人。如果要检查给定名称是否在 Outlook 中(作为联系人或在 GAL 中),只需调用 Application.Session.CreateRecipient
,然后调用 Recipient.Resolve
。如果调用 returns true
,您可以读取 Recipient.Address
和各种其他属性。
问题已解决!
感谢 answers 我可以生成这个最小的 Python 代码来演示我想要实现的目标:
from __future__ import print_function
import win32com.client
search_string = 'Doe John'
outlook = win32com.client.gencache.EnsureDispatch('Outlook.Application')
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)
当搜索字符串有多个匹配项时,@Prof.Falken 的解决方案中的方法并不总是有效。我找到了另一个解决方案,它更强大,因为它使用 displayname
的精确匹配。
它的灵感来自 How to fetch exact match of addressEntry object from GAL (Global Address List)。
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)
我有一份名单,其中一些是完整的,一些是运行分类的。我想在 Outlook 地址列表中搜索这些名称的匹配项。
我最接近的是这个 Python 代码 which came from ActiveState Code,但它不搜索全局地址,只搜索我的(本地?)列表,其中有 3 个地址,这是显然不对。应该有几千条记录。
欢迎任何提示。我用谷歌搜索并阅读了几十页,但没有定论。我宁愿不直接连接到 LDAP,我认为这在我的组织中是违反政策的,而且我不确定这是否可能。如果可能,希望通过 Outlook API 执行此操作。
DEBUG=1
class MSOutlook:
def __init__(self):
self.outlookFound = 0
try:
self.oOutlookApp = \
win32com.client.gencache.EnsureDispatch("Outlook.Application")
self.outlookFound = 1
except:
print("MSOutlook: unable to load Outlook")
self.records = []
def loadContacts(self, keys=None):
if not self.outlookFound:
return
# this should use more try/except blocks or nested blocks
onMAPI = self.oOutlookApp.GetNamespace("MAPI")
ofContacts = \
onMAPI.GetDefaultFolder(win32com.client.constants.olFolderContacts)
if DEBUG:
print("number of contacts:", len(ofContacts.Items))
for oc in range(len(ofContacts.Items)):
contact = ofContacts.Items.Item(oc + 1)
if contact.Class == win32com.client.constants.olContact:
if keys is None:
# if we were't give a set of keys to use
# then build up a list of keys that we will be
# able to process
# I didn't include fields of type time, though
# those could probably be interpreted
keys = []
for key in contact._prop_map_get_:
if isinstance(getattr(contact, key), (int, str, unicode)):
keys.append(key)
if DEBUG:
keys.sort()
print("Fields\n======================================")
for key in keys:
print(key)
record = {}
for key in keys:
record[key] = getattr(contact, key)
if DEBUG:
print(oc, record['FullName'])
self.records.append(record)
随机links:
MSDN - How to create a Global Address List programmatically using Visual Basic
Infinitec - How to get the Global Address List programatically
Return Boolean True - Downloading the Global Address List from Outlook/Exchange
Grokbase - [python-win32] getting global addressbook with extended mapi
Stack Overflow - Searching Outlook Global Address
再次尝试列表失败
Stack Overflow - Retrieving outlook Contacts via python
这是我得到上面link的地方
Stack Overflow - Fetching Outlook Contacts with Python
根本运行
Python for Windows - Automating Microsoft Outlook
只是默认地址簿。另外我要搜索,不一一列举。
如果有人能提出解决方案,我不介意它是 C++、VB、Perl、Python 等
您上面的代码处理默认联系人文件夹中的联系人。如果要检查给定名称是否在 Outlook 中(作为联系人或在 GAL 中),只需调用 Application.Session.CreateRecipient
,然后调用 Recipient.Resolve
。如果调用 returns true
,您可以读取 Recipient.Address
和各种其他属性。
问题已解决!
感谢
from __future__ import print_function
import win32com.client
search_string = 'Doe John'
outlook = win32com.client.gencache.EnsureDispatch('Outlook.Application')
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)
当搜索字符串有多个匹配项时,@Prof.Falken 的解决方案中的方法并不总是有效。我找到了另一个解决方案,它更强大,因为它使用 displayname
的精确匹配。
它的灵感来自 How to fetch exact match of addressEntry object from GAL (Global Address List)。
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)