[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 application architect and part time lecturer by current professions who enjoys dabbling in software development, RPA, IOT, advanced analytics, data engineering and business intelligence. I am aspiring to complete a PhD degree in Computer Science within the next three 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.

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

  1. Hi,

    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)

    Like

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 )

Facebook photo

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

Connecting to %s

%d bloggers like this: