connect-to-exchange-mailbox-with-python/3072491....I have refereed the following link to connect to Exchange Online and download attachments and read mails on windows(using Python and exchangelib library). Now I want to accomplish the same task on CentOS but when I manually download the exchangelib
library and install it.
Whenever I try to import exchangelib, it throws an error like:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "exchangelib/__init__.py", line 2, in <module>
from .account import Account # noqa
File "exchangelib/account.py", line 8, in <module>
from cached_property import threaded_cached_property
ImportError: No module named cached_property
What might be the problem?
My main objective is to read emails and download them. No imap/pop3 server address is available. Is there an alternative to exchangelib
?
from exchangelib import DELEGATE, Account, Credentials
credentials = Credentials(
username='MYWINDOMAIN\\myusername',
password='topsecret'
)
account = Account(
primary_smtp_address='john@example.com',
credentials=credentials,
autodiscover=True,
access_type=DELEGATE
)
# Print first 100 inbox messages in reverse order
for item in account.inbox.all().order_by('-datetime_received')[:100]:
print(item.subject, item.body, item.attachments)
I have used this code in Windows. Help me out with Linux.
exchangelib
depends on various 3rd party packages, so you can't just download and import the package. You need to install it usingpip
to get these packages installed automatically:This is how you read all emails and store all attachments with
exchangelib
:Related: How can I send an email with an attachment with Python and Microsoft Exchange?