简体   繁体   English

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

[英]Parse the body of attachment in outlook mail with MAPI Python

I am using win32com for parsing emails in my outlook, how can I parse the contents of attachment in mail. 我正在使用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

I want to get the contents of attachment, not able to find any proper documentation for this. 我想获取附件的内容,无法找到任何适当的文档。 Any help in guiding me to correct direction is highly appreciated. 非常感谢您指导我正确方向的任何帮助。

Firstly, you might want to retrieve the Inbox using 首先,您可能想使用以下方式检索收件箱

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

Secondly, looping through all messages in a folder is a terrible idea - use Items.Find/FindNext or Items.Restrict with a restriction on the Subject property.. 其次,循环遍历文件夹中的所有消息是一个糟糕的主意-使用Items.Find/FindNextItems.Restrict并限制Subject属性。

Thirdly, Outlook would not let you directly access attachment contents, you would need to save the attachment as a file ( Attachment.SaveAsFile ), and then read the file contents. 第三,Outlook不允许您直接访问附件内容,您需要将附件另存为文件( Attachment.SaveAsFile ),然后读取文件内容。 If you want to access attachment contents directly, you can use Redemption - it exposes Attachment / RDOAttachment . 如果您想直接访问附件内容,则可以使用Redemption-它公开了Attachment / RDOAttachment AsText / AsArray / AsStream properties. AsText / AsArray / AsStream属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM