I got a request from a client who constantly gets CVs and have to download them for the hiring managers to review them and wanted to get some way an automated mechanism of downloading all those emails.
Googling lead me to the exchangelib project which is a great python source for such a purpose. I built my local lab of the following servers to test it
I built my local Certification authority and made sure that all servers has the CA installed to avoid any issues on python.
If you are going to use this on your production environment you can basically install Python anywhere even on your own computer and it should work if EWS is exposed and Autodiscover is configured propely and have a valid and trusted 3rd party Certificate.
However, If you would like to schedule this to work on a daily basis and let it download attachments from mailbox then you’ll need a server or at least a computer to rely on that it would be on when the scheduled task works.
Windows Server 2016:
from exchangelib import DELEGATE, IMPERSONATION, Account, Credentials, EWSDateTime, EWSTimeZone, Configuration, NTLM, GSSAPI, CalendarItem, Message, Mailbox, Attendee, Q, ExtendedProperty, FileAttachment, ItemAttachment, HTMLBody, Build, Version, FolderCollection
credentials = Credentials(username='moh10ly\info', password='Bc12345$')
ews_url = 'https://mail.moh10ly.com/EWS/exchange.asmx' ews_auth_type = 'NTLM' primary_smtp_address = 'info@moh10ly.com' config = Configuration(service_endpoint=ews_url, credentials=credentials, auth_type=ews_auth_type)
account = Account( primary_smtp_address=primary_smtp_address, config=config, autodiscover=False, access_type=DELEGATE)
import os.path from exchangelib import Account, FileAttachment, ItemAttachment, Message some_folder = account.inbox for item in some_folder.all(): for attachment in item.attachments: if isinstance(attachment, FileAttachment): local_path = os.path.join('/temp', attachment.name) with open(local_path, 'wb') as f: f.write(attachment.content)
This by now should be working fine and you should see that it is saving all your mailbox attachments to the folder that you have configured in the path section of the code.
Working config from exchangelib import DELEGATE, IMPERSONATION, Account, Credentials, EWSDateTime, EWSTimeZone, Configuration, NTLM, GSSAPI, CalendarItem, Message, Mailbox, Attendee, Q, ExtendedProperty, FileAttachment, ItemAttachment, HTMLBody, Build, Version, FolderCollection credentials = Credentials(username='moh10ly\info', password='Bc12345$') ews_url = 'https://mail.moh10ly.com/EWS/exchange.asmx' ews_auth_type = 'NTLM' primary_smtp_address = 'info@moh10ly.com' config = Configuration(service_endpoint=ews_url, credentials=credentials, auth_type=ews_auth_type) account = Account( primary_smtp_address=primary_smtp_address, config=config, autodiscover=False, access_type=DELEGATE) import os.path from exchangelib import Account, FileAttachment, ItemAttachment, Message some_folder = account.inbox for item in some_folder.all(): for attachment in item.attachments: if isinstance(attachment, FileAttachment): local_path = os.path.join('/temp', attachment.name) with open(local_path, 'wb') as f: f.write(attachment.content) ----------------- #To download all attachments in the inbox: for item in account.inbox.all(): for attachment in item.attachments: if isinstance(attachment, FileAttachment): local_path = os.path.join('/sky', attachment.name) with open(local_path, 'wb') as f, attachment.fp as fp: buffer = fp.read(1024) while buffer: f.write(buffer) buffer = fp.read(1024) print('Saved attachment to', local_path)
Hope this have helped you
https://ecederstrand.github.io/exchangelib/
https://pypi.org/project/exchangelib/
Reset and manage your Active Directory users' Passwords Active Directory is one of the most…
Finding Exchange Database hidden mailboxes. Story:Maybe you have been in this situation before, trying to…
If you're using a Proxy server in your firewall or in your network and have…
Story:I got some clients that have reported some of their users being locked out and…
Delegate Permissions This is a code that I have wrote recently to check if an…
550 relay not permitted distribution group contact Symptoms When trying to add an external contact…
View Comments
In your example, you are able to save all attachments that are FileAttachment types. I'm running into an issue where some of my attachments are ItemAttachment types. It doesn't understand "f.write(attachment.content)".
How would you be able to save the ItemAttachment?