Search code examples
emailoutlookcountpywin32win32com

pywin32/win32com unable to pull more than 1537 emails at a time


I am working with pywin32/win32com to try and pull the sender information of ALL emails in my inbox. My code works, but only ever shows 1537 emails no matter who runs it. I have 2-3k emails in my default folder in my inbox, and a coworker has closer to 20k emails in his default inbox folder, but both of us run this script and only see the same number, 1537. Can someone please fill me in as to what could be causing this limitation?

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
rootlist = {}
inbox = outlook.GetDefaultFolder(6) #6 = Inbox (without mails from the subfolder)
messages = inbox.Items
PidTagSenderSmtpAddress = "http://schemas.microsoft.com/mapi/proptag/0x5D01001F"

for i in range(0, messages.Count):
    messaging = inbox.Items[i]
    if messaging.Class == 43 or messaging.Class == 69: #detect if mailitem
        sender = messaging.PropertyAccessor.GetProperty(PidTagSenderSmtpAddress)
        #print(sender)
        if sender in rootlist:
            rootlist[str(sender)][0] = rootlist[str(sender)][0] + 1
        else:
            rootlist[str(sender)] = [1]


Solution

  • Never loop through all items in a folder, use Items.Find/FindNext or Items.Restrict with a query like

    @SQL="http://schemas.microsoft.com/mapi/proptag/0x5D01001F" = '[email protected]'