Python 读取outlook本地邮箱各目录实现

from win32com.client.gencache import EnsureDispatch as Dispatch
import Debug


debug_print=Debug.print_debug_info

class Email(object):
    def __init__(self):
        self.outlook = Dispatch("Outlook.Application")
        self.mapi = self.outlook.GetNamespace("MAPI")
        self.Accounts = self.mapi.Folders
        self.item = []

    def parse_mail(self):
        debug_print("")
        if (hasattr(self.item, 'ReceivedTime')):  
            debug_print('ReceivedTime' + ": " + str(self.item.ReceivedTime))
        if (hasattr(self.item, 'SenderName')):
            debug_print('SenderName' + ": " + self.item.SenderName)
        if (hasattr(self.item, 'Subject')):
            debug_print('Subject' + ": " + self.item.Subject)
        if (hasattr(self.item, 'Body')):
           debug_print('Body' + ": " + self.item.Body)
        if (hasattr(self.item, 'To')):
            debug_print('To' + ": " + self.item.To)
        if (hasattr(self.item, 'CC')):
            debug_print('CC' + ": " + self.item.CC)

    def read_mail(self):
        for Account in self.Accounts:
            debug_print(Account.Name)
            
            # 只查找需要的邮箱账号信息
            if Account.Name == 'xxx@xxx.com':
                Folders = Account.Folders
                for Folder in Folders:
                    
                    #只需要收件箱的邮件
                    debug_print(Folder.Name)
                    if Folder.Name == '收件箱':
                        Items = Folder.Items
                        debug_print(len(Items))
                        Items.Sort("[ReceivedTime]", True)
                        count = 1
                        for self.item in Items:
                            Debug.print_debug_info(str(count) + ":")
                            self.parse_mail()
                            Debug.print_debug_info("\n")
                            count += 1


if __name__ == '__main__':
    mail = Email()
    mail.read_mail()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值