You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python读取Outlook邮件技术问询:如何读取所有已接收邮件(含已读、未读及已移动至其他文件夹的邮件)

Solution: Access All Received Emails (Including Moved Ones) in Outlook with Python

Got it, let's fix this! Your current code only targets the top-level Inbox folder, which is why it misses emails you've moved to subfolders or other mail folders. To access every received email across all relevant folders, we need to recursively traverse through all mail folders (and their subfolders) in Outlook.

Why Your Current Code Fails

The line inbox = outlook.GetDefaultFolder(6) only points to the root Inbox folder. It doesn't include any subfolders you've created, nor other mail folders like Archive, Sent Items, or custom folders where you might have moved emails.

Step-by-Step Solution

We'll use a recursive function to iterate through every folder, process its mail items, and then dive into its subfolders. Here's the complete code:

import win32com.client

def process_mail_folder(folder):
    """Recursively process all mail items in a folder and its subfolders."""
    # Iterate through all items in the current folder
    for item in folder.Items:
        # Only process mail items (skip meetings, tasks, etc.)
        if item.Class == 43:  # olMailItem constant value
            # Extract and process email details
            print(f"Subject: {item.Subject}")
            print(f"Received Time: {item.ReceivedTime}")
            print(f"Body Preview: {item.Body[:150]}...\n")  # Print first 150 chars of body
            # Add your custom processing logic here (e.g., save to file, parse content)
    
    # Recursively process all subfolders of the current folder
    for subfolder in folder.Folders:
        process_mail_folder(subfolder)

if __name__ == "__main__":
    # Connect to Outlook
    outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
    
    # Option 1: Scan all folders under the default Inbox (including subfolders)
    print("Scanning all folders under Inbox...")
    inbox = outlook.GetDefaultFolder(6)  # 6 = olFolderInbox
    process_mail_folder(inbox)
    
    # Option 2: Scan ALL mail folders in your Outlook account (including Archive, Sent, etc.)
    # print("\nScanning all mail folders in Outlook...")
    # for account_folder in outlook.Folders:
    #     process_mail_folder(account_folder)

Key Details Explained

  • Recursive Folder Traversal: The process_mail_folder function first processes all items in the current folder, then calls itself on each subfolder—this ensures no folder is missed.
  • Mail Item Check: item.Class ==43 filters out non-mail items (like calendar events or tasks) since we only care about emails.
  • Flexible Scanning: Use Option 1 if you only want emails from the Inbox and its subfolders. Uncomment Option 2 to scan every folder in your entire Outlook account.

Important Notes

  • Ensure Outlook is running when you execute the script (win32com interacts with the running Outlook instance).
  • If you have shared mailboxes or delegated folders, you may need to access them explicitly using outlook.Folders["Shared Mailbox Name"].
  • For large mailboxes, consider adding a filter (e.g., folder.Items.Restrict("[ReceivedTime] >= '01/01/2024'")) to limit the items processed and improve performance.

内容的提问来源于stack exchange,提问作者Echchama Nayak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.01 01:44:10