Open ritvika-coder opened 2 months ago
import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
def get_smtp_address(address_entry): smtp_address = None
try:
# First, try to get the SMTP address using GetExchangeUser()
if address_entry.AddressEntryUserType == 0: # olExchangeUser
exchange_user = address_entry.GetExchangeUser()
if exchange_user:
smtp_address = exchange_user.PrimarySmtpAddress
elif address_entry.AddressEntryUserType == 1: # olExchangeDistributionList
exchange_dl = address_entry.GetExchangeDistributionList()
if exchange_dl:
smtp_address = exchange_dl.PrimarySmtpAddress
except Exception:
pass
if not smtp_address:
# Fallback: use PropertyAccessor to retrieve the SMTP address
try:
property_accessor = address_entry.PropertyAccessor
smtp_address = property_accessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x39FE001E")
except Exception as e:
print(f"Could not retrieve SMTP address for {address_entry.Name}: {e}")
return smtp_address
def get_dl_members(dl): members = []
if dl.AddressEntryUserType == 1: # Exchange distribution list
try:
if dl.Members:
for i in range(dl.Members.Count):
member = dl.Members.Item(i + 1)
if member.AddressEntryUserType == 0: # Regular user (Exchange user)
smtp_address = get_smtp_address(member)
if smtp_address:
members.append(smtp_address)
elif member.AddressEntryUserType == 1: # Another distribution list
try:
nested_dl = member.GetExchangeDistributionList()
if nested_dl:
members.extend(get_dl_members(nested_dl)) # Recurse into nested DL
except Exception as e:
print(f"Error retrieving members of nested DL {member.Name}: {e}")
except AttributeError:
print(f"{dl.Name} does not have direct members.")
elif dl.AddressEntryUserType == 0: # Regular user (Exchange user)
smtp_address = get_smtp_address(dl)
if smtp_address:
members.append(smtp_address)
return members
address_book = outlook.AddressLists.Item("Global Address List")
distribution_list_name = "Your Distribution List Name"
try: distribution_list = address_book.AddressEntries.Item(distribution_list_name) exchange_dl = distribution_list.GetExchangeDistributionList() # Get DL as Exchange DL
if exchange_dl:
members = get_dl_members(exchange_dl)
print(f"Email addresses of members in {distribution_list_name}:")
for member in members:
print(member)
else:
print(f"Distribution list '{distribution_list_name}' not found or has no members.")
except Exception as e: print(f"Error finding distribution list: {e}")
import win32com.client
Initialize Outlook application
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
Function to recursively get all members of a distribution list
def get_dl_members(dl): members = []
Get the Outlook address book (Global Address List)
address_book = outlook.AddressLists.Item("Global Address List")
Replace 'Your Distribution List Name' with the actual distribution list name
distribution_list_name = "Your Distribution List Name"
Find the distribution list
try: distribution_list = address_book.AddressEntries.Item(distribution_list_name) exchange_dl = distribution_list.GetExchangeDistributionList() # Get DL as Exchange DL
except Exception as e: print(f"Error finding distribution list: {e}")