首页 > 解决方案 > 使用 Python 在 GAL 中更快地搜索电子邮件

问题描述

我需要创建一个 Python 脚本,该脚本根据他们的电子邮件获取有关 1500 个 Outlook 联系人(共 20000 个)的不同信息。到目前为止,我设法做到了:

def grab_user_details(email):
    first_name, last_name, department, location = '', '', '', ''
    outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
    gal = outlook.Session.GetGlobalAddressList()
    entries = gal.AddressEntries
    for i in entries:
        user = i.GetExchangeUser()
        if user is not None:
            if user.PrimarySmtpAddress == email:
                first_name = email.split("@")[0].split(".")[0].title()
                last_name = email.split("@")[0].split(".")[1].title()
                department = user.Department
                location = user.OfficeLocation
                print(email, first_name, last_name, department, location)
                break

最后,代码只是针对该特定电子邮件遍历 GAL。找到它后,它会中断,并继续搜索下一封电子邮件。这种方法对于以 A 或至少 B 开头的电子邮件来说很快……但是当您有一个包含 20000 封电子邮件的 GAL 时,您不能只等待 2 天就可以完成整个字母表。

有没有更快的方法来做到这一点?

谢谢!

标签: pythonemailoutlookgal

解决方案


使用类的CreateRecipient方法Namespace根据电子邮件地址获取 Recipient 类的实例。

Sub TestRecipient()
 Dim myNamespace As Outlook.NameSpace 
 Dim myRecipient As Outlook.Recipient 
 
 Set myNamespace = Application.GetNamespace("MAPI") 
 Set myRecipient = myNamespace.CreateRecipient("address@domain.com") 
 
 myRecipient.Resolve 
 
 If myRecipient.Resolved Then 
 
   ' do some stuff here
 
 End If 
 
End Sub 

Recipient.AddressEntry属性返回AddressEntry对应于已解析收件人的对象。访问该AddressEntry属性会强制解析未解析的收件人姓名。如果无法解析名称,则返回错误。如果接收者已解析,则Resolved属性为True

然后,您可以使用AddressEntry.GetExchangeUser方法返回一个 ExchangeUser 对象,该对象表示该对象AddressEntry是否AddressEntry属于AddressList诸如全局地址列表 (GAL) 之类的 Exchange 对象并对应于 Exchange 用户。


推荐阅读