'How to read the body of an outlook email using Python / Win32Client?

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


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source