python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > python读取本地邮件

python办公自动化之读取本地OUTLOOK客户端邮件

作者:ryanlll3

这篇文章主要为大家详细介绍了如何使用python实现读取本地OUTLOOK客户端邮件,文中的示例代码讲解详细,具有一定的参考价值,需要的小伙伴可以了解下

本方法适用于POP服务器不可用,管理员没有赋予开启权限的情况。你只能读取本地客户端的邮件,否则建议直接开发python程序自动登录服务器,读取邮件。

安装依赖包

python -m pip install pypiwin32

读取邮件

#!/usr/bin/python
# _*_ coding:utf-8 _*_
 
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]
            else:
                ReceivedTime_1 = ''
            # 发件人
            if hasattr(xx, 'SenderName'):
                SenderName_1 = xx.SenderName
            else:
                SenderName_1 = ''
            # 收件人
            if hasattr(xx, 'To'):
                to_to_1 = xx.To
            else:
                to_to_1 = ''
            # 抄送人
            if hasattr(xx, 'CC'):
                cc_cc_1 = xx.CC
            else:
                cc_cc_1 = ''
            # 主题
            if hasattr(xx, 'Subject'):
                Subject_1 = xx.Subject
            else:
                Subject_1 = ''
            # 邮件MessageID
            if hasattr(xx, 'EntryID'):
                MessageID_1 = xx.EntryID
            else:
                MessageID_1 = ''
            # 会话主题
            if hasattr(xx, 'ConversationTopic'):
                ConversationTopic_1 = xx.ConversationTopic
            else:
                ConversationTopic_1 = ''
            # 会话ID
            if hasattr(xx, 'ConversationID'):
                ConversationID_1 = xx.ConversationID
            else:
                ConversationID_1 = ''
            # 会话记录相对位置
            if hasattr(xx, 'ConversationIndex'):
                ConversationIndex_1 = xx.ConversationIndex
            else:
                ConversationIndex_1 = ''
            # 邮件内容
            if hasattr(xx, 'Body'):
                EmailBody_1 = xx.Body
            else:
                EmailBody_1 = ''
 
        # 判断当前查询的一级邮件目录是否有二级目录(若有多级目录,可以参考此段代码)
        if Level_1_Name.Folders:
            # 二级目录的集合
            Level_2_Names = Level_1_Name.Folders
            for Level_2_Name in Level_2_Names:
                print(' - - 正在查询二级目录:' , Level_1_Name.Name , '//' , Level_2_Name.Name)
                # 二级目录的邮件集合
                Mail_2_Messages = Level_2_Name.Items
                # 开始查看单个邮件的信息
                for yy in Mail_2_Messages:
                    # 记录根目录名称
                    Root_Directory_Name_2 = Account_Name.Name
                    # 记录一级目录名称
                    Level_1_FolderName_2 = Level_1_Name.Name
                    # 记录二级目录名称
                    Level_2_FolderName_2 = Level_2_Name.Name
                    # 接收时间
                    if hasattr(yy, 'ReceivedTime'):
                        ReceivedTime_2 = str(yy.ReceivedTime)[:-6]
                    else:
                        ReceivedTime_2 = ''
                    # 发件人
                    if hasattr(yy, 'SenderName'):
                        SenderName_2 = yy.SenderName
                    else:
                        SenderName_2 = ''
                    # 收件人
                    if hasattr(yy, 'To'):
                        to_to_2 = yy.To
                    else:
                        to_to_2 = ''
                    # 抄送人
                    if hasattr(yy, 'CC'):
                        cc_cc_2 = yy.CC
                    else:
                        cc_cc_2 = ''
                    # 主题
                    if hasattr(yy, 'Subject'):
                        Subject_2 = yy.Subject
                    else:
                        Subject_2 = ''
                    # 邮件MessageID
                    if hasattr(yy, 'EntryID'):
                        MessageID_2 = yy.EntryID
                    else:
                        MessageID_2 = ''
                    # 会话主题
                    if hasattr(yy, 'ConversationTopic'):
                        ConversationTopic_2 = yy.ConversationTopic
                    else:
                        ConversationTopic_2 = ''
                    # 会话ID
                    if hasattr(yy, 'ConversationID'):
                        ConversationID_2 = yy.ConversationID
                    else:
                        ConversationID_2 = ''
                    # 会话记录相对位置
                    if hasattr(yy, 'ConversationIndex'):
                        ConversationIndex_2 = yy.ConversationIndex
                    else:
                        ConversationIndex_2 = ''
                    # 邮件正文内容
                    if hasattr(yy, 'Body'):
                        EmailBody_2 = yy.Body
                    else:
                        EmailBody_2 = ''
        else:
            pass

保存附件

from win32com.client.gencache import EnsureDispatch as Dispatch
 
# 获取收件箱目录(数字6代表收件箱)
outlook_accounts = Dispatch('Outlook.Application').GetNamespace('MAPI').GetDefaultFolder(6)
# 收件箱下所有邮件
mails = outlook_accounts.Items
# 邮件按时间排序
mail.Sort('[ReceivedTime]', True)
# 获取第6封邮件的所有附件(下标从1开始)
atts = mails.Item(6)Attachments
# 第6封邮件附件数量
atts_num = atts.Count
# 遍历附件对象
for att in atts:
    print('att_name: {}'.format(att.FileName))
    # 将附件存入D盘
    att.SaveAsFile('D:\{}'.format(att.FileName))

到此这篇关于python办公自动化之读取本地OUTLOOK客户端邮件的文章就介绍到这了,更多相关python读取本地邮件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文