Equinox23
Equinox23

Reputation: 436

python win32com bulk reading outlook emails - too many open items

i'm facing some issues while trying to fetch a bulk mail via python wincom32.client.

Basically, it seems like there's a limit on the number of items that could be opened on a single session, and that is a server-side flag or status.. the problem is that i didn't find out any way to resume/close/re-set and i can't ask for sysadmins to do it for me..

Here's my code snippet - i know it's ugly - but i'm using it on a jupiter notebook just to experiment/play around before arranging it down properly.

import win32com.client

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

def create_message_list(folder):
    return [{
        "folder": folder.Name,
        "sender" : m.Sender,
        "recipients" : m.Recipients,
        "subject" : m.subject, 
        "body":m.body
        } for m in folder.Items]

for folder in outlook.Folders:
    if (folder.Name=="myfolder.name"):
        message_list = create_message_list(folder)

and here's what i get:

com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', '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).

Notice that i had my sketch code working for a while. i could fetch something like 10k messages before it messed up with that error.

Things i've been trying (with no result) - try to close the folder / mapi session - running a for loop on the same items and calling the message.Close(0) - using the GetFirst() / GetLast() methods on message instead of list comprehension

thanks anyone for ideas/suggestions/whatever.

Upvotes: 0

Views: 2536

Answers (3)

Anirudh
Anirudh

Reputation: 11

First of all thanks Equinox23 for your answer.

Just wanted to add few more points to it. The same thing happened for me while accessing bundles of outlook emails. Consider list1 and list2, for every iteration I get the output list1 and finally, append it to list2 and reset the list1. After 230+ iterations, I got the same error. Here, the list2 has reference for list1 as Equinox23 mentioned and doesn't allow us to open multiple items at a time.

So, instead of directly appending list1 to list2 (like list2.append(list1)), I did the following as Equinox23 mentioned (list2.append(str(list1))) [Use the appropriate datatype like str(list1)/int(list1) and append it].

You can eliminate the error and can get the desired output.

Upvotes: 1

Equinox23
Equinox23

Reputation: 436

the problem was due to using dictionary to store message data held somehow a reference to it, without letting the resource to be released, even if an explicit m.Close(0) was invoked.

I've replaced them all with a "dictkey" : str(m.<field>) call and the error does not show up anymore.

Upvotes: 2

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66255

You keep all items in a folder open - that is a really bad idea. Store only the entry ids, and reopen the messages on demand using Namespace.GetItemFromID. As soon as you are done with the item, release it.

Upvotes: 0

Related Questions