首页 > 其他分享 >爬取outlook邮件

爬取outlook邮件

时间:2022-11-26 12:22:06浏览次数:41  
标签:outlook 爬取 msg attachment print microsoft 邮件

背景:工作中遇到需求,获取outlook邮箱中某段时间的邮件附件,本地存储

import win32com.client as win32
import os


outlook = win32.Dispatch('outlook.application')  # 获取outlook句柄

mapi = outlook.GetNamespace('MAPI')   # 获取API

messages = mapi.Folders("[email protected]").Folders("收件箱").Items  # 收件箱所有邮件

WoYaoDeYouJian = messages.Restrict("[ReceivedTime] >= '" + "01/01/2019 00:00 AM" + "'")   # 筛选出2019年至今的邮件


outputDir = r"D:\attachment"
try:
    for message in list(messages):
        try:
            s = message.sender
            for attachment in message.Attachments:
                attachment.SaveASFile(os.path.join(outputDir, attachment.FileName))
                print(f"attachment {attachment.FileName} from {s} saved")
        except Exception as e:
            print(str(e))
except Exception as e:
    print(str(e))


#     print(msg.Subject) #这是邮件标题
#     print(msg.Attachments)  # 这是邮件附件
#     print(msg.Body)  # 这是邮件主体内容
#     print(msg.sender)  # 这是邮件发件人


# (2)微软官方.NET接口文档(英文)
# https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.outlook?view=outlook-pia
# (3)微软官方.NET接口文档(中文)
# https://docs.microsoft.com/zh-cn/dotnet/api/microsoft.office.interop.outlook?view=outlook-pia

  

标签:outlook,爬取,msg,attachment,print,microsoft,邮件
From: https://www.cnblogs.com/quegai18/p/16927216.html

相关文章