2

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('test@mail.com', 'pwd') as mailbox:
    subjects = [msg.subject for msg in mailbox.fetch()]

Is there a way to access that second shared mailbox?

Zeitounator
  • 38,476
  • 7
  • 53
  • 66
Jonas
  • 1,749
  • 1
  • 10
  • 18

1 Answers1

2

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())]
Zeitounator
  • 38,476
  • 7
  • 53
  • 66
  • Thanks for your answer. The second mailbox is not a second E-Mail-Adress, but it's a mailbox that is linked to my main E-Mail-Adress. So I'm allowed (among other users) to read that second Mailbox. – Jonas Apr 26 '21 at 18:44
  • 1
    Are you talking about a shared microsoft exchange mailbox ? If yes, [it look like it is possible](https://serverfault.com/a/222615/519003) with my above suggestion. Just use the correctly crafted identifier as specified in that previous link. If no, you will have to give more details about that linked mailbox, and your question probably belongs to https://superuser.com anyway. – Zeitounator Apr 26 '21 at 21:14
  • Yes I am talking about the microsoft exchange mailbox. The hint you linked in your comment got me to the right solution. If I change the login-part to: "login('IMAP-SERVER\USER\SECOND-MAILBOX', 'PW')" it works like a charm. If you want to add this solution to your answer, i can mark it as solved. Many thanks! – Jonas Apr 27 '21 at 06:27
  • 1
    I edited answer and question since this is absolutely specific to exchange. Since I have absolutely no way to test that and you now have an experience, feel free to fix the connection server and login string (which I adapted from the original answer) to the one you actually used and worked. Cheers. – Zeitounator Apr 28 '21 at 06:58