如何通过Python读取本地Outlook中第二个账号的第一封邮件?
读取Outlook第二个邮箱的第一封邮件实现方案
实现思路
- 通过
mapi.Accounts获取所有已配置的Outlook账户 - 定位到第二个账户(Python序列从0开始计数,所以取索引1的元素)
- 获取该账户对应的收件箱文件夹
- 读取收件箱内的第一封邮件(Outlook默认按接收时间排序,可按需调整)
完整代码
import win32com.client # 初始化Outlook连接 outlook = win32com.client.Dispatch('outlook.application') mapi = outlook.GetNamespace("MAPI") # 转换账户集合为列表,方便索引定位 accounts = list(mapi.Accounts) # 定位到第二个目标账户 target_account = accounts[1] # 获取该账户的收件箱(6是Outlook收件箱的固定常量标识) inbox = target_account.DeliveryStore.GetDefaultFolder(6) # 读取第一封邮件 if inbox.Items.Count > 0: first_email = inbox.Items[0] # 打印邮件核心信息示例 print("主题:", first_email.Subject) print("发件人:", first_email.SenderName) print("接收时间:", first_email.ReceivedTime) print("正文内容:", first_email.Body) else: print("该账户收件箱无邮件")
补充说明
- 若需要按最新接收时间排序邮件,可在获取收件箱Items后添加排序逻辑:
inbox.Items.Sort("[ReceivedTime]", True) # True代表降序排列 - Outlook常用文件夹常量:3=已发送邮件,4=草稿,5=已删除邮件
内容的提问来源于stack exchange,提问作者Junior programming
相关产品推荐
相关产品推荐

