Python Outlook: Read Inbox of additional mailbox
Asked Answered
P

6

7

I'm using Outlook 2010 - and have my main mailbox: [email protected]

I have also added another mailbox to my profile: mb data proc

Both appear as top level folders within Outlook:

[email protected]
-Inbox
-Sent Items
-Deleted Items

mb data proc
-Inbox
-Sent Items
-Deleted Items

I cannot create a different profile for the additional mailbox. It has been added in the same profile.

How do I get a reference to the Inbox in the "mb data proc" mailbox?

This is the same problem as described here Get reference to additional Inbox but this in VBS.

How to do in python?

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
folder=outlook.Folders("mb data proc")
msg=folder.Items
msgs=msg.GetLast()
print msgs    

I tried this but I get this error:

       folder=outlook.Folders("mb data proc")
AttributeError: _Folders instance has no __call__ method
Primo answered 8/4, 2014 at 9:8 Comment(0)
O
5

I had a similar doubt and as I understand it the solution stated here is for Python 2.7

I will try to make it understandable regarding how to operate it using Python 3.+ versions.

import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
folder = outlook.Folders.Item("Mailbox Name")
inbox = folder.Folders.Item("Inbox")
msg = inbox.Items
msgs = msg.GetLast()
print (msgs)
print (msgs.Subject)

Since _Folder is not callable, you need to use Folders.Item() method in Python 3+ to reference your mailbox.

Hope that was helpful. Thanks!

Okajima answered 16/7, 2019 at 12:57 Comment(0)
U
3

Here's a simple solution. I think the only part you missed was getting to the "Inbox" folder inside of "mb data proc".

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
folder = outlook.Folders("mb data proc")
inbox = folder.Folders("Inbox")
msg = inbox.Items
msgs = msg.GetLast()
print msgs
Urbannal answered 22/12, 2016 at 16:58 Comment(0)
S
2

If your looking for other mailboxes or seperate PST files you have access to in outlook, try using the Store / Stores MAPI objects.

import win32com.client

for stor in win32com.client.Dispatch("Outlook.Application").Session.Stores:
    print( stor.DisplayName) 

PS .Session return the same reference as .GetNamespace("MAPI")

for reference https://learn.microsoft.com/en-us/office/vba/api/overview/outlook

Showman answered 12/2, 2019 at 22:0 Comment(0)
M
2

I was trying to access Additional Mail Boxes and read the Inbox from these Shared folders

import win32com.client

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

>>> folder = outlook(1)

>>> inbox = folder.Folders("Inbox")

>>> message = inbox.Items

>>> messages = message.GetLast()

>>> body_content = messages.body

>>> print (body_content)
Molliemollify answered 7/4, 2019 at 10:55 Comment(0)
D
0

Thank you for you Posts! Here is a function I pulled together based on your Input, to read out the available Folders:

This is my first post, so I hope I copied the code in properly:

def check_shared(namespace,recip = None): 
    """Function takes two arguments:
        .) Names-Space: e.g.: 
            which is set in the following way: outlook = Dispatch("Outlook.Application").GetNamespace("MAPI") and
        .) Recipient of an eventual shared account as string: e.g.: Shared e-Mail adress is "[email protected]"
            --> This is optional --> If not added, the standard-e-Mail is read out"""


    if recip is None:
        for i in range(1,100):                           
            try:
                inbox = namespace.GetDefaultFolder(i)     
                print ("%i %s" % (i,inbox))            
            except:
                #print ("%i does not work"%i)
                continue
    else:
        print('The folders from the following shared account will be printed: '+recip)
        tmpRecipient = outlook.CreateRecipient(recip)
        for i in range(1,100):                           
            try:
                inbox = namespace.GetSharedDefaultFolder(tmpRecipient, i)     
                print ("%i %s" % (i,inbox))            
            except:
                #print ("%i does not work"%i)
                continue
    print("Done")
Dunstan answered 30/6, 2016 at 16:25 Comment(0)
H
-1

Firstly, you can use Namespace.GetSharedDefaultFolder method.

Secondly, then line

folder=outlook.Folders("mb data proc")

needs to be

folder=outlook.Folders.Item("mb data proc")

Hughmanick answered 8/4, 2014 at 16:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.