Extracting Embedded Images From Outlook Email

前端 未结 1 1741
萌比男神i
萌比男神i 2021-01-03 08:21

I am using Microsoft\'s CDO (Collaboration Data Objects) to programmatically read mail from an Outlook mailbox and save embedded image attachments. I\'m trying to do this f

1条回答
  •  伪装坚强ぢ
    2021-01-03 08:31

    Difference in versions of OS/Outlook/CDO is what might be the source of confusion, so here are the steps to get it working on WinXP/Outlook 2007/CDO 1.21:

    • install CDO 1.21
    • install win32com.client
    • goto C:\Python25\Lib\site-packages\win32com\client\ directory run the following:
    python makepy.py
    • from the list select "Microsoft CDO 1.21 Library (1.21)", 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 file 3FA7DEA7-6438-101B-ACC1-00AA00423326x0x1x33.py that's just been generated, will give you an idea of what classes, methods, properties and constants are available.

    Now that we are done with 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()
    

    Same general approach will also work if you have older version of CDO (CDO for win2k)

    0 讨论(0)
提交回复
热议问题