使用 MAPI 解析 outlook 邮件中的附件正文 Python

Parse the body of attachment in outlook mail with MAPI Python

我在outlook中使用win32com解析邮件,如何解析邮件中附件的内容。

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
accounts= win32com.client.Dispatch("Outlook.Application").Session.Accounts
inbox = outlook.Folders(accounts['<Account-Name>'].DeliveryStore.DisplayName)
messages = inbox.Folders['Inbox'].Items
if len(messages) > 0:
   for message2 in messages:
       title = message2.Subject
       if title == '<Title-of-mail>':
          attachment = message2.Attachments.Item(1)
          print(attachment)
          print(message2.Body)
          # print(attachment.Body) //Error

我想获取附件的内容,找不到任何合适的文档。非常感谢任何指导我正确方向的帮助。

首先,您可能希望使用

检索收件箱

accounts['<Account-Name>'].DeliveryStore.GetDefaultFolder(olFolderInbox)

其次,遍历文件夹中的所有邮件是一个糟糕的主意 - 使用 Items.Find/FindNextItems.Restrict 并限制主题 属性..

第三,Outlook不会让您直接访问附件内容,您需要将附件另存为文件(Attachment.SaveAsFile),然后读取文件内容。如果要直接访问附件内容,可以使用 Redemption (I am its author) - it exposes Attachment / RDOAttachment.AsText / AsArray / AsStream properties.