[python-win32] how to get mails from Outlook

Tim Golden mail at timgolden.me.uk
Fri Dec 21 09:42:21 CET 2007


Antony Joseph wrote:
> hi,
> 
> I am trying to read all the mails in the Inbox from outlook and convert
> those mails into text format in my local path.
> 
> Is there any tutorials or any links that can be usefull to me.

Here's an absolutely Noddy example to get you going:

<code>
#
# inbox.py
# test case for access to Exchange; run through the
#   hard-coded user's inbox and list the subjects of each email.
#

import win32com.client

session = win32com.client.gencache.EnsureDispatch ("MAPI.Session")
session.Logon ()

inbox = session.Inbox
messages = inbox.Messages
message = messages.GetFirst ()
while message:
   print "subject: ", message.Subject
   message = messages.GetNext ()

session.Logoff ()

</code>

and, aside from the MSDN docs on MAPI.Session, the keyword
you're looking for is CDO. Here's a couple of useful sites:

http://www.outlookcode.com/article.aspx?id=20
http://www.cdolive.com/default.htm

BTW, this is all relatively old stuff (although it still works
fine on my XP-Outlook2003-Exchangewhatever setup). There may
well be some "preferred" MSActiveNETMailProvider [*] technique
which is more future-proofed.

If you need more info feel to come back and ask.

TJG

[*] Invented, obviously; don't go searching for it!


More information about the python-win32 mailing list