Espanta
Espanta

Reputation: 1140

Get email address of members of an Exchange Distribution List - Python

I used win32.client and could successfully access members of an exchange distribution list using python. However, because there are two users with the same first and last name, I would like to be able to access their email address instead of the name.

Using below loop, I can go through the members of Exchange Distribution List and print the name of all members:

import win32com.client

outlook_obj = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") 

#This function gets outlook object and retuens all the members of ALL Groups
address_lists = outlook_obj.AddressLists

#Access Exchange Distribution Lists
dist_lists = address_lists['All Distribution Lists']
return(dist_lists)

dl_index = a_numerical_index_greater_than_zero # you can try different numbers until you find the index of your desired distributionList, or loop thorough all the members and find what you are looking for
for m in dist_lists.AddressEntries.Item(dl_index).GetExchangeDistributionList().Members:
    print(str(m))

The above script perfectly works and prints out all the name of all the members of the distribution list. However, I am looking for distinct email address of the members, as I see names are not distinct (I can have two people with the same name Jack Smith, but [email protected] and [email protected] are still distinct).

I used the object definition from this source to build above code, but it seems I am unable to connect members to their email address.

Appreciate any help!

Upvotes: 2

Views: 4103

Answers (1)

Espanta
Espanta

Reputation: 1140

Okay - I got my answer and I am sharing in case others may need this.

Indeed below script is returning the addressEntry of the Member

dist_lists.AddressEntries.Item(dl_index).GetExchangeDistributionList().Members[0].GetExchangeUser()

and addressEntry can give you access to all details of the account, including email address. Below is the exact code to fetch email address of the user

dist_lists.AddressEntries.Item(dl_index).GetExchangeDistributionList().Members[0].GetExchangeUser().PrimarySmtpAddress

Upvotes: 1

Related Questions