[Automation] Retrieving a List of Mail Senders From a Shared Mailbox (Using Outlook and Python)

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.

Published by Jacqui Muller

I am an automation architect by current profession who enjoys dabbling in software development, RPA, IOT, advanced analytics, data engineering and business intelligence. I am aspiring to complete a Master's degree in Computer Science within the next two years. My competencies include a high level of computer literacy as well as programming in various languages. I am passionate about my field of study and occupation as I believe it has the ability and potential to impact lives - both drastically and positively. I come packaged with an ambition to succeed and make the world a better place.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: