I was struggling with this for days, so I figured I should post the code so others can see how to do it.
The following script will read the last email in an Outlook mailbox and save the attachments. It uses the CDO COM Interface to interact with Outlook/Exchange.
#!/usr/bin/env python # Read the last email in an Outlook mailbox and save the attachments. from win32com.client import Dispatch def main(): session = Dispatch('MAPI.session') #session.Logon('Outlook') # for local mailbox session.Logon('','',0,1,0,0,'exchange.foo.com\nusername'); inbox = session.Inbox message = inbox.Messages.GetLast() attachments = message.Attachments for i in range(attachments.Count): attachment = attachments.Item(i + 1) # indexes are 1 based filename = 'c:\\tempfile_%i' % i attachment.WriteToFile(filename) session.Logoff() if __name__ == '__main__': main()
* Extra Special thanks to Sergey Golovchenko for helping out.
4 comments:
Sorry you had to struggle :) Did you post to the python/python-win32 lists? I think that question's been answered a couple of times before. Here's my take:
http://timgolden.me.uk/python/win32_how_do_i/replace-outlook-attachments-with-links.html
Very good solution, thank you.
Gergő
Hi Corey,
Thanks for the solution. Normally it would have helped me, but unfortunately my attachments were of type 4 :-(
Just FYI: I'd like to refer to this discussion:
http://objectmix.com/microsoft-exchange/280339-cdo-attachments.html
My aim was to extract some information from my attachments. Of course that is still possible as is pointed out in the above-mentioneed discussion, but I'm leaving it lying down for the mean time. Regards, Dobedani
How do we get the name of the attachment in the Email.Checked the Oject model but could not find it.
Post a Comment