Level of Difficulty: Beginner – Senior.
Have you ever had a use case where you have a shared mailbox setup on your Outlook but you don’t have the credentials to the account behind the mailbox (if one exists)? Well you can get around that by running the Python script detailed in this post.
The approach to this solution is to use Outlook, which should already be set up on your computer, with Python to pull a list of email addresses of senders and write it to Excel.
For this, you’ll need to install the Pandas library, only if you want to write the addresses and display names to Excel:
import win32com.client import pandas as pd # Outlook Connection outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") folder = outlook.Folders.Item("@<Mailbox Name>") inbox = folder.Folders.Item("Inbox") messages = inbox.Items # Initialise Lists senders =  addresses =  # Iterate through messages for sender name and address for message in messages: try: if message.Class==43: if message.SenderEmailType=='EX': if message.Sender.GetExchangeUser() != None: addresses.append(message.Sender.GetExchangeUser().PrimarySmtpAddress) else: addresses.append(message.Sender.GetExchangeDistributionList().PrimarySmtpAddress) else: addresses.append(message.SenderEmailAddress) senders.append(message.Sender) except Exception as e: print(e) # Create Excel file with results df = pd.DataFrame() df['Sender'] = senders df['Address'] = addresses df.to_excel('Addresses.xlsx')
Did this work for you? Feel free to drop a comment below with suggestions or questions.
2 thoughts on “[Automation] Retrieving a List of Mail Senders From a Shared Mailbox (Using Outlook and Python)”
I’m getting below error when trying to access a shared folder:
-2147352567, ‘Exception occurred.’, (4096, ‘Microsoft Outlook’, ‘The attempted operation failed. An object could not be found.’, None, 0, -2147221233), None)
Hi Nishant, does the folder exist? Did you add it under Inbox or on the same level of Inbox?