#!/usr/bin/env python
# -*- coding=utf-8 -*-
# @Filename :GetOutlook.py
# @Time :2020/11/24 14:13
# @Author :binwz
from win32com.client.gencache import EnsureDispatch as Dispatch
# 固定写法
outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
# 根级目录(邮箱名称,包括Outlook读取的存档名称)
Accounts = outlook.Folders
for Account_Name in Accounts:
print('>> 正在查询的帐户名称:{}'.format(Account_Name.Name))
# 一级目录集合(与inbox同级)['收件箱', '发件箱', '已发送', 'xxx', 'xxx']
Level_1_Names = Account_Name.Folders
for Level_1_Name in Level_1_Names:
print('- 正在查询一级目录:{}'.format(Level_1_Name.Name))
# 一级文件夹的mail合集[所有邮件对象]
Mail_1_Messages = Level_1_Name.Items
# 将邮件按接收时间排序
# Mail_1_Messages.Sort("[ReceivedTime]", True)
# 开始查看单个邮件的信息
for xx in Mail_1_Messages:
Root_Directory_Name_1 = Account_Name.Name # 记录根目录名称
Level_1_FolderName_1 = Level_1_Name.Name # 记录一级目录名称
Level_2_FolderName_1 = '' # 一级目录肯定没有二级目录,顾留为空
# 接收时间
# if (hasattr(xx, 'ReceivedTime')):
# ReceivedTime_1 = str(xx.ReceivedTime)[:-6