Extract embedded images from Outlook Email

I use Microsoft CDO (Collaboration Data Objects) to programmatically read mail from an Outlook mailbox and save attachments. I am trying to do this with Python using Win32 extensions, but examples in any language that uses CDO will be useful.

I'm still here ...

The following Python code will read the last email in my inbox, print the attachment names, and print the message body:

from win32com.client import Dispatch session = Dispatch('MAPI.session') session.Logon('','',0,1,0,0,'exchange.foo.com\nbar'); inbox = session.Inbox message = inbox.Messages.Item(inbox.Messages.Count) for attachment in message.Attachments: print attachment print message.Text session.Logoff() 

However, attachment names are such as: "zesjvqeqcb_chart_0". Inside the email source, I see the image source links as follows: <IMG src = "cid: zesjvqeqcb_chart_0">

So, can this CID URL (or something else) be used to retrieve the actual image and save it locally?

+4
source share
1 answer

The difference in OS / Outlook / CDO versions is what can be a source of confusion, so here are the steps to help it work with WinXP / Outlook 2007 / CDO 1.21:

  • install CDO 1.21
  • install win32com.client
  • Go to the C: \ Python25 \ Lib \ site-packages \ win32com \ client \ directory, do the following:
  python makepy.py 
  • select "Microsoft CDO 1.21 Library (1.21)" from the list, click "OK"
  C: \ Python25 \ Lib \ site-packages \ win32com \ client> python makepy.py
 Generating to C: \ Python25 \ lib \ site-packages \ win32com \ gen_py \ 3FA7DEA7-6438-101B-ACC1-00AA00423326x0x1x33.py
 Building definitions from type library ...
 Generating ...
 Importing module 
  • Examining the 3FA7DEA7-6438-101B-ACC1-00AA00423326x0x1x33.py file that has just been generated will give you an idea of ​​which classes, methods, properties, and constants are available.

Now that we have finished the boring steps, here is the fun part:

 import win32com.client from win32com.client import Dispatch session = Dispatch('MAPI.session') session.Logon ('Outlook') # this is profile name inbox = session.Inbox messages = session.Inbox.Messages message = inbox.Messages.GetFirst() if(message): attachments = message.Attachments for i in range(attachments.Count): attachment = attachments.Item(i + 1) # yep, indexes are 1 based filename = "c:\\tmpfile" + str(i) attachment.WriteToFile(FileName=filename) session.Logoff() 

The same general approach will also work if you have an older version of CDO installed (CDO for win2k)

+5
source

All Articles