栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

读取OUTLOOK邮箱文件的两种方法(离线和在线)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

读取OUTLOOK邮箱文件的两种方法(离线和在线)

离线读取:

import win32com.client
import os

dir = r"C:Users30367Desktop1"

file_list = os.listdir(dir)

for file in file_list:
    if file.endswith(".msg"):
        outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
        msg = outlook.OpenSharedItem(dir + "/" + file)
        print(msg.Body)

从邮箱读取:

def read_outlook_mailbox():
    """连接Outlook邮箱,读取收件箱内的邮件内容"""
    # 使用MAPI协议连接Outlook
    account = Dispatch('Outlook.Application').GetNamespace('MAPI')

    # 获取收件箱所在位置
    inbox = account.GetDefaultFolder(6)  # 数字6代表收件箱
    # 获取收件箱下的所有邮件
    mails = inbox.Items
    mails.Sort('[ReceivedTime]', True)  # 邮件按时间排序

    # 读取收件箱内前3封邮件的所有信息(下标从1开始)
    for index in range(1, 4):
        print('正在读取第[{}]封邮件...'.format(index))
        mail = mails.Item(index)
        alltext = mail.Body
        #print(alltext)
        print(type(alltext))
        #print('邮件正文内容:{}'.format(mail.Body))

        pattern=re.compile('POL(.+)r')   #匹配ab与ef之间的内容
        POL=pattern.findall(alltext)
        POL = str(POL).replace(":","").replace("[","").replace("]","").replace("'","")
        print(POL)

参考:Python连接Outlook读取邮件和发送邮件

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/948531.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号