代码之家  ›  专栏  ›  技术社区  ›  Manvi

使用MAPI python分析Outlook邮件中的附件正文

  •  0
  • Manvi  · 技术社区  · 6 年前

    我正在使用win32com在我的Outlook中解析电子邮件,如何解析邮件中附件的内容。

    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
    

    我想得到附件的内容,找不到任何合适的文件。任何帮助我纠正方向的帮助都是非常感谢的。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Dmitry Streblechenko    6 年前

    首先,您可能希望使用

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

    其次,循环浏览文件夹中的所有邮件是一个糟糕的主意-使用 Items.Find/FindNext Items.Restrict 对标的物有限制。

    第三,Outlook不允许您直接访问附件内容,您需要将附件保存为文件( Attachment.SaveAsFile ,然后读取文件内容。如果要直接访问附件内容,可以使用 Redemption -它暴露 Attachment / RDOAttachment . AsText / AsArray / AsStream 性质。