Search code examples
pythonimap-tools

Is it possible to adress a second shared exchange mailbox with imap-tools?


Using imap-tools, it's perfectly possible to access and work with my main exchange mailbox account. But I've got a second mailbox (it's not just a different folder within my main INBOX, but a different shared exchange mailbox, which I have access to).

from imap_tools import MailBox, AND
    
# get list of email subjects from INBOX folder
with MailBox('imap.mail.com').login('[email protected]', 'pwd') as mailbox:
    subjects = [msg.subject for msg in mailbox.fetch()]

Is there a way to access that second shared mailbox?


Solution

  • As reported in this answer, getting a connection to an exchange shared mailbox is just a particular case of a regular IMAP connection. Simply open a second connection with the correct login for the shared mailbox.

    from imap_tools import MailBox
    from itertools import chain
    
    # get list of email subjects from both INBOX folder
    with MailBox('imap.mail.com').login('userlogin', 'userpwd') as main_box, \
         MailBox('imap.mail.com').login('DOMAIN\userlogin\sharedboxname', 'userpwd') as shared_box:
        subjects = [msg.subject for msg in chain(main_box.fetch(), shared_box.fetch())]