Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading e-mails from Outlook with Python through MAPI

I'm trying to write a short program that will read in the contents of e-mails within a folder on my exchange/Outlook profile so I can manipulate the data. However I'm having a problem finding much information about python and exchange/Outlook integration. A lot of stuff is either very old/has no docs/not explained. I've tried several snippets but seem to be getting the same errors. I've tried Tim Golden's code:

import win32com.client  session = win32com.client.gencache.EnsureDispatch ("MAPI.Session")  # # Leave blank to be prompted for a session, or use # your own profile name if not "Outlook". It is also # possible to pull the default profile from the registry. # session.Logon ("Outlook") messages = session.Inbox.Messages  # # Although the inbox_messages collection can be accessed # via getitem-style calls (inbox_messages[1] etc.) this # is the recommended approach from Microsoft since the # Inbox can mutate while you're iterating. # message = messages.GetFirst () while message:     print message.Subject     message = messages.GetNext () 

However I get an error:

pywintypes.com_error: (-2147221005, 'Invalid class string', None, None) 

Not sure what my profile name is so I tried with:

session.Logon() 

to be prompted but that didn't work either (same error). Also tried both with Outlook open and closed and neither changed anything.

like image 228
johnharris85 Avatar asked Feb 22 '11 11:02

johnharris85


People also ask

Can Python read emails from Outlook?

Learn how you can use IMAP protocol to extract, parse and read emails from outlook, aol, office 365 and other email providers as well as downloading attachments using imaplib module in Python.

What is MAPI in Python?

mapi (Metadata API) is a python library which provides a high-level interface for media database providers, allowing users to efficiently search for television and movie metadata using a simple interface.

How do I read email inbox in Python?

We used Google App Password to connect our Python script to the Gmail account, so our Python program could read the email from the inbox. You do not need to do it if you are using a different email provider or server. There, you can log in to your account just with your email id and password with the Python program.


1 Answers

I had the same problem you did - didn't find much that worked. The following code, however, works like a charm.

import win32com.client  outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")  inbox = outlook.GetDefaultFolder(6) # "6" refers to the index of a folder - in this case,                                     # the inbox. You can change that number to reference                                     # any other folder messages = inbox.Items message = messages.GetLast() body_content = message.body print body_content 
like image 179
Bobby Avatar answered Sep 21 '22 13:09

Bobby