ritvika-coder / CoderRitvik

0 stars 0 forks source link

Ruruur #2

Open ritvika-coder opened 2 months ago

ritvika-coder commented 2 months ago

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 = []

# Check if the distribution list has a valid Members attribute
if dl.AddressEntryUserType == 1:  # Exchange distribution list
    try:
        if dl.Members:  # Some DLs have Members property
            for i in range(dl.Members.Count):
                member = dl.Members.Item(i + 1)
                if member.AddressEntryUserType == 0:  # Regular user (Exchange user)
                    members.append(member.Name)
                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 (e.g., Exchange user)
    members.append(dl.Name)

return 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

if exchange_dl:
    members = get_dl_members(exchange_dl)
    print(f"Members of {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}")

ritvika-coder commented 2 months ago

import win32com.client

Initialize Outlook application

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

Function to get the SMTP address of an address entry

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

Function to recursively get all members of a distribution list and their SMTP addresses

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

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

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}")