biker007
biker007

Reputation: 21

Extract outlook email body and recipient email address using python

I am trying to extract outlook email body, recipient address, subject and received date.

I was able to extract subject and received date but unable to extract body and recipient address:

Below is my code for subject and received date:

outlook = win32com.client.Dispatch('Outlook.Application').GetNamespace('MAPI')

namespace = outlook.Session

recipient = namespace.CreateRecipient("[email protected]")

inbox = outlook.GetSharedDefaultFolder(recipient, 6)

messages = inbox.Items

email_subject = []

email_date = []

email_date_time = []


for x in messages:
    sub = x.Subject
    received_date = x.senton.date()
    received_date_time = str(x.ReceivedTime)
    email_subject.append(sub)
    email_date.append(received_date)
    email_date_time.append(received_date_time)

For body i am trying:

for x in messages:
    body = x.Body
    print(body)

but this is not working and i am getting the error below:

Traceback (most recent call last):

  File "<ipython-input-85-d79967933b99>", line 2, in <module>
    sub = x.Body

  File "C:\ProgramData\Anaconda3\lib\site-packages\win32com\client\dynamic.py", line 516, in __getattr__
    ret = self._oleobj_.Invoke(retEntry.dispid,0,invoke_type,1)

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

Upvotes: 2

Views: 7967

Answers (3)

Dan
Dan

Reputation: 1

Recalled Emails would have no Body hence we can find that via the MessageClass and exclude that particular Class

for i in messages:
    if email.MessageClass != "IPM.Outlook.Recall":

Upvotes: 0

Rory Hughes
Rory Hughes

Reputation: 46

I think that I found a solution that worked. For me it was an issue with permissions, but I made the registry edits in https://www.slipstick.com/developer/change-programmatic-access-options/ and it worked a treat.

EDIT: I think this worked by unblocking some lower level permissions that enabled an outside program to access the outlook client.

Upvotes: 1

David Zemens
David Zemens

Reputation: 53623

I've just run similar code on my computer, in an inbox with 3,000+ items of mixed type (skype message notifications, calendar invites/notifications, emails, etc.) and I cannot replicate this error, even for items where not m.Body -- I thought that was a possible culprit, maybe a certain type of item doesn't have a body would throw an error -- but that appears to not be the case:

>>> for m in messages:
...     if not m.body:
...         print(m.Subject)
...         print(m.Body)
...
Accepted: Tables discussion

Message Recall Failure: tables/ new data status

Message Recall Failure: A few issues with the data

You should probably add a print(m.Class) because I still think that maybe certain types of items do not have a Body property.

This thread suggests that there may be a user/security setting that prevents programmatic access to Outlook, so you might want to double-check on that (though I think if not allowed, none of your code would work -- still, worth looking in to!).

I have figured out the source of this error. We are running into issues with the comObjectModelGaurd. Our group policy recently changed to disallow programatic access to protected mailItem objects.

Modifying Outlook Users Trust settings or the registry will fix the problem.

Since I can't replicate the error, perhaps I can still help you debug and identify the source of the problem, from which we can probably come up with a good solution.

Use a function to get the item's body, and use try/except to identify which items are causing error.

def getBody(m):
     s = ""
     try:
         s = m.Body
     except COMError:
         s = '\t'.join(('Error!', m.Class, m.senton.date(), m.Subject))
     return s


for m in messages:
    print(getBody(m))

Upvotes: 1

Related Questions