Reputation: 211
Im trying to use win32 module for python and get the body of my email message. However whenever I try to get it i get an error for any email body I try to get. I am able to get the message subject/timerecieved/etc however I just can't seem to get the body.
import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
message = messages.GetLast()
body_content = message.body
---------------------------------------------------------------------------
com_error Traceback (most recent call last)
<ipython-input-41-bdd6c5734297> in <module>()
1 messages = inbox.Items
2 message = messages.GetLast()
----> 3 body_content = message.body#.encode('ascii', 'ignore').decode('ascii')
4 #print (body_content)
C:\Users\e659383\Documents\portableapps\WinPython-64bit-3.4.3.5\python-3.4.3.amd64\lib\site-packages\win32com\client\dynamic.py in __getattr__(self, attr)
509 debug_attr_print("Getting property Id 0x%x from OLE object" % retEntry.dispid)
510 try:
--> 511 ret = self._oleobj_.Invoke(retEntry.dispid,0,invoke_type,1)
512 except pythoncom.com_error as details:
513 if details.hresult in ERRORS_BAD_CONTEXT:
com_error: (-2147467259, 'Unspecified error', None, None)
I get same error for any email message I try to get the body.
Upvotes: 0
Views: 2545
Reputation: 2053
Not 100% sure, but perhaps body should use a capital B as it seems to be referencing a C# property.
https://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook._mailitem.body.aspx
So:
body_content = message.Body
Upvotes: 1