import win32com.client def process_outlook_emails_from_root(): # Connect to Outlook outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") # Get the root folder of the mailbox root_folder = outlook.Folders.Item(1) # Typically the first item is the root # Recursive function to process all folders def process_folder(folder): print(f"\nProcessing folder: {folder.Name}") # # Iterate through all items in the current folder # for item in folder.Items: # if item.Class == 43: # 43 is the class for MailItem # print(f"Found email - Subject: {item.Subject}") # print(f" From: {item.SenderName}") # print(f" Received: {item.ReceivedTime}") # # Add your email processing logic here # Recursively process subfolders for subfolder in folder.Folders: process_folder(subfolder) # Start processing from the root folder process_folder(root_folder) if __name__ == "__main__": process_outlook_emails_from_root()