pythonmapigal

Using MAPI to retrieve the list of emails from GAL


This is my code so far:

import win32com.client

o = win32com.client.gencache.EnsureDispatch("Outlook.Application")
ns = o.GetNamespace("MAPI")

adrLi = ns.AddressLists.Item("Global Address List")
contacts = adrLi.AddressEntries
numEntries = adrLi.AddressEntries.Count

nameAliasDict = {}

for i in contacts:
    name = i.Name
    alias = i.Address.split("=")[-1]
    print i.GetExchangeUser().PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3A56101E")

I"m getting the property from: https://msdn.microsoft.com/en-us/library/bb446002.aspx

But for some reasons, I get this error:

pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, u'Microsoft Outlook', u'The property "http://schemas.microsoft.com/mapi/proptag/0x3A550003" is unknown or cannot be found.', None, 0, -2147221233), None)

Am I doing this wrong?


Solution

  • You cannot assume that PR_CONTACT_EMAIL_ADDRESSES or any other MAPI property will be available. Your code must expect and handle that error from PropertyAccessor.GetProperty. Check if you can actually see the property on that particular object in OutlookSpy (I am its author - click IAddrBook, "Open Root Container" etc.).

    Why exactly do you need that property? If you just need the SMTP address. use ExchangeUser.PrimarySmtpAddress.