0
votes

I have some code that monitors an inbox, and outputs the subject line every time a new email is recieved. For reference, I am using Outlook. The issue is that it only monitors my default inbox. Does anyone know how to get it to monitor other inboxes as well?

import win32com.client
import pythoncom
import re

class Handler_Class(object):
    def OnNewMailEx(self, receivedItemsIDs):
        for ID in receivedItemsIDs.split(","):
            mail = outlook.Session.GetItemFromID(ID)
            subject = mail.Subject
            print(subject)

outlook = win32com.client.DispatchWithEvents("Outlook.Application", Handler_Class)

pythoncom.PumpMessages()

For reference, I have two mailboxes, lets say one is Personal and the other is Shared

I would like for the script to only monitor new emails in the Shared inbox, but right now it only does so for my Personal inbox.

I have tried making the following changes to the outlook definition in the script to monitor the mailbox. While it didn't throw an error, it simply didn't pick up on any new mail items in that box:

outlook = win32com.client.DispatchWithEvents("Outlook.Application", Handler_Class).GetNamespace("MAPI").Folders.Item("Shared")

Does anyone know how to work around this? Thanks!

2

2 Answers

0
votes

The NewMailEx event is fired for every received item that is processed by Microsoft Outlook. If you want to get notifications for items from a specific account you may check out the recipient (To) and simply ignore others.

0
votes

NewMail / NewMailEx events only fires on the default Inbox folder. If you want to monitor the Inbox folder from other mailboxes, open the other account's store using either the Namespace.Stores collection or, if the mailbox is not shown in Outlook but you have the right to access it, using Namespace.CreateRecipient / Namespace.OpenSharedDefaultFolder(recipieent, olFolderInbox). You can then retrieve the Inbox folder of that store using Store.GetDefaultFolder, and use the Items.ItemAdd event (where Items is returned from the MAPIFolder.Items property).