0

How can I read the body of an outlook email using python? The first code below allows me to access the folder but it continues to return the message subject only. I simply need the body of the message.

import win32com.client
import datetime
import os
import email
import pandas as pd
import os
import glob

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6).Folders.Item("Eres")
message = inbox.items

for message in inbox.Items:
        if message.unread == True:
    
         print(message)

This code below does not work at all and generates the following error..


com_error Traceback (most recent call last) in 16 17 message = messages.GetLast() ---> 18 body_content = message.body

~\Anaconda3\lib\site-packages\win32com\client\dynamic.py in getattr(self, attr) 514 debug_attr_print("Getting property Id 0x%x from OLE object" % retEntry.dispid) 515 try: --> 516 ret = self.oleobj.Invoke(retEntry.dispid,0,invoke_type,1) 517 except pythoncom.com_error as details: 518 if details.hresult in ERRORS_BAD_CONTEXT:

com_error: (-2147467259, 'Unspecified error', None, None)

import win32com.client
import datetime
import os
import email
import pandas as pd
import os
import glob

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6).Folders.Item("Eres")
messages = inbox.Items

message = messages.GetLast()
body_content = message.body
  • It isn’t guaranteed that `message` is a `MailItem` object, so maybe check that `message.Class` is 43 (constant olMail). Also (and this may not make a difference) try using `Body` with an uppercase ‘B’. – DS_London Feb 23 '22 at 07:20
  • Hey DS_London, thank you for replying. I chcked class and it is 43 and I have tried Body with the uppercase. It works fine on my personal computer so I think this is a firewall issue on my work computer. – Shane Nicholson Feb 23 '22 at 10:13

1 Answers1

-1

"com_error: (-2147467259, 'Unspecified error', None, None)" This is a security issue. Likely cause by your antivirus. Solution: Win32com Outlook Body Properties Error

ryda
  • 38
  • 5
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/32009466) – Patrick Yoder Jun 18 '22 at 17:07