[Python] Reading Outlook mail in Python or Powershell

Discussão em 'Python' iniciado por Stack, Maio 8, 2025.

  1. Stack

    Stack Membro Participativo

    I have been trying to read the content of mails in Outlook from both Python and Powershell, but for every version of code I have tried, I can't access the properties I want.

    I can get the com-objects and I can access the subject from each. But I need to get the body and receivers. In Powershell the properties are $null, and in Python I get:

    com_error(-2147467260, 'Åtgärden avbröts', None, None)

    I have tried:

    • having Outlook opened
    • one or no mail selected in Outlook
    • saving a mail seperately and reading that specific file

    I have read many articles explaining how to access the files, but none of them worked, and never found a proper explanation for what might be the problem.

    Unfortunately I can not use MS Graph or the older Powershell modules for accessing Exchange to access the inbox and mail.

    This is the latest version I tried.


    import win32com.client

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

    for f in outlook.Folders('my@mail.se').Folders:
    if f.Name == "Inkorg":
    try:
    items = f.Items.Restrict("[MessageClass] = 'IPM.Note'")
    items.Sort("[ReceivedTime]", True)
    count = items.Count
    print(f"Found {count} items in 'Inkorg' folder.")
    for i in range(1, count + 1):
    try:
    item = items.Item(i)
    # Filter only mail items
    if item.Class == 43: # olMailItem = 43
    print(f"Subject: {item.Subject}")
    sender = item.SenderName if item.SenderName else "Sender not found"
    html_body = item.HTMLBody if item.HTMLBody else "HTMLBody not found"
    print(f"Sender: {sender}")
    print(f"Body (HTML):\n{html_body[:200]}...")
    except Exception as e:
    print(f"Error accessing item {i}: {e}")
    except Exception as e:
    print(f"Error accessing folder '{f.Name}': {e}")

    Continue reading...

Compartilhe esta Página