'How do I monitor a different folder (than the default) within Outlook for new mail (and then fire an event) with Python
I have the below code
import win32com.client
import pythoncom
import time
class Handler_Class(object):
def OnNewMailEx(self, receivedItemsIDs):
for ID in receivedItemsIDs.split(","):
mailItem = outlook.GetNamespace("MAPI").GetItemFromID(ID)
print(mailItem.Subject)
outlook = win32com.client.DispatchWithEvents("Outlook.Application", Handler_Class)
print("[" + time.strftime("%H:%M:%S %d/%m/%Y") + "] " + " Running...")
pythoncom.PumpMessages()
The above python code monitors my default Outlook inbox (when Outlook is open) and fires an event when a new mail comes in. This code works fine for the default Outbox inbox, but I'd like to rather monitor a shared inbox rather than the default one. The shared email address is '[email protected]' and then i want to look inside 'inbox' folder.
I tried the below code to try get it to monitor for new mail on the shared inbox in Outlook but it seems not to be working...
Does anyone have any ideas?
outlook = win32com.client.DispatchWithEvents("Outlook.Application", Handler_Class).GetNamespace("MAPI").Folders['[email protected]'].Folders['Inbox']
Solution 1:[1]
First of all, you need to get the default inbox folder for the shared mailbox. Use the NameSpace.GetSharedDefaultFolder method which returns a Folder object that represents the specified default folder for the specified user. This method is used in a delegation scenario, where one user has delegated access to another user for one or more of their default folders (for example, their shared Inbox folder). The following VBA sample shows how to use this method:
Sub ResolveName()
Dim myNamespace As Outlook.NameSpace
Dim myRecipient As Outlook.Recipient
Dim CalendarFolder As Outlook.Folder
Set myNamespace = Application.GetNamespace("MAPI")
Set myRecipient = myNamespace.CreateRecipient("Eugene Astafiev")
myRecipient.Resolve
If myRecipient.Resolved Then
Call ShowCalendar(myNamespace, myRecipient)
End If
End Sub
Sub ShowCalendar(myNamespace, myRecipient)
Dim CalendarFolder As Outlook.Folder
Set CalendarFolder = myNamespace.GetSharedDefaultFolder(myRecipient, olFolderCalendar)
CalendarFolder.Display
End Sub
After you have got the folder reference you can set up the ItemAdd event on the Items collection of that folder. The Items.ItemAdd event is fired when one or more items are added to the specified collection. Note, this event does not run when a large number of items are added to the folder at once (more than sixteen).
Solution 2:[2]
I did it this way. I know is not elegant nor professional but it works:
outlook = win32com.client.DispatchWithEvents("Outlook.Application", Handler_Class).GetNamespace("MAPI").Folders['[email protected]'].Folders['Inbox'].Folders['the folder name you need to monitor']
If you need the items in this you can add at the end .Items but I think you already know that :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Eugene Astafiev |
| Solution 2 | geodor |
