diff --git a/exchangelib/folders/base.py b/exchangelib/folders/base.py index a8b5647a..d9af1029 100644 --- a/exchangelib/folders/base.py +++ b/exchangelib/folders/base.py @@ -96,7 +96,8 @@ def __init__(self, **kwargs): self.item_sync_state = kwargs.pop("item_sync_state", None) self.folder_sync_state = kwargs.pop("folder_sync_state", None) super().__init__(**kwargs) - if self._distinguished_id and self.account: + if self._distinguished_id and not self._distinguished_id.mailbox and self.account: + # Ensure that distinguished IDs have a mailbox, but don't override a custom mailbox (e.g. shared folders) self._distinguished_id.mailbox = Mailbox(email_address=self.account.primary_smtp_address) @property diff --git a/tests/test_folder.py b/tests/test_folder.py index 13556afa..dde0c224 100644 --- a/tests/test_folder.py +++ b/tests/test_folder.py @@ -500,6 +500,18 @@ def test_get_folders(self): ).get_folders() ) + def test_shared_folders(self): + # Test that the custom 'email_address' is not overwritten. The test account does not have access to any shared + # accounts, so we can't expand the test to actual queries. + shared_folder = Folder( + root=self.account.root, + _distinguished_id=DistinguishedFolderId( + id=Inbox.DISTINGUISHED_FOLDER_ID, + mailbox=Mailbox(email_address="foo@example.com"), + ), + ) + self.assertEqual(shared_folder._distinguished_id.mailbox.email_address, "foo@example.com") + def test_folder_grouping(self): # If you get errors here, you probably need to fill out [folder class].LOCALIZED_NAMES for your locale. for f in self.account.root.walk():