Link to home
Start Free TrialLog in
Avatar of Danny Osborne
Danny Osborne

asked on

Python win32com to iterate though outlook mail folder

Hi,

I'm using the below code to loop through mail items in a shared outlook mail folder. The problem is, mail items appear to remain as state 'open' by the server. This means, once I loop through 250 emails (default max open items for mail server), the code exits with the following message even though I've called the mail item close method -

com_error: (-2147352567, 'Exception occurred.', (4096, u'Microsoft Outlook', u'Your server administrator has limited the number of items you can open simultaneously. Try closing messages you have opened or removing attachments and images from unsent messages you are composing.', None, 0, -2147220731), None).

import win32com.client
import win32com
import os
import pickle
outlook = win32com.client.Dispatch("outlook.Application").GetNameSpace("MAPI")

inbox = outlook.Folders("Ibis2").Folders("Inbox").Folders("06-2016")

message = inbox.items
infolist = []
for message2 in message:
    #message2=message.GetLast()
    subject=message2.Subject
    #date1=message2.senton.Date()
    sender = message2.Sender
    receipt = message2.ReceivedTime
    print receipt, " | ", subject, " | ", sender
    infolist.append((receipt, subject, sender))
    message2.Save
    message2.Close(0)
fp = open("C:\Python27\\emails.pkl","w")
pickle.dump(infolist, fp)
fp.close() 

Open in new window


There's a server size registry fix for this issue, but I don't have access to the mail server. Any idea how I can get around this on my local machine?
ASKER CERTIFIED SOLUTION
Avatar of Thiago Soares
Thiago Soares

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Danny Osborne
Danny Osborne

ASKER

Hi,

Thanks for looking into this. Yes it does appear to be done server side. Not sure why the server tags each email as open until no more can be opened. I would have thought opening reading then closing would be ok, but it's obviously an MS exchange quirk.

Yes you're right about getting the months data, I want to create some kind of metric to monitor mail coming in and going out.

Breaking it down into days or even hours is a great idea. I'll give that a go. If I can make it work I'll post the code.
As initially thought, the solution to this problem probably requires thinking outside of the box which Thiago has started off for me.

I'm sure there are a number of people who are trying to gather data on more than 250 emails in one go, so if I manage to get something going, I'll post the code.