I had to use exchangelib, to modify the subject of some of my emails. The following is the line of code that I am using to modify the email subjects. I am using App details with the exchangelib package.
credentials = OAuth2Credentials(client_id="", client_secret="",
tenant_id="",
identity=Identity(primary_smtp_address=""))
config = Configuration(credentials=credentials, auth_type=OAUTH2,
service_endpoint='https://outlook.office365.com/EWS/exchange.asmx')
account = Account(
primary_smtp_address=mailbox_name,
config=config,
access_type=IMPERSONATION)
inbox = account.inbox
if inbox.all().exists():
for message in inbox.all().order_by('-datetime_received')[:10]:
message.subject = "Modified Subject"
# message.save()
message.save(update_fields=['subject'])
The update message operation is throwing the below error:
message.save(update_fields=['subject'])
File "C:\Program Files\Python38\lib\site-packages\exchangelib\items\item.py", line 97, in save
item_id, changekey = self._update(
File "C:\Program Files\Python38\lib\site-packages\exchangelib\util.py", line 38, in wrapper
return f(self, *args, **kwargs)
File "C:\Program Files\Python38\lib\site-packages\exchangelib\items\item.py", line 183, in _update
res = UpdateItem(account=self.account).get(
File "C:\Program Files\Python38\lib\site-packages\exchangelib\services\common.py", line 93, in get
res = list(self.call(**kwargs))
File "C:\Program Files\Python38\lib\site-packages\exchangelib\services\common.py", line 550, in _pool_requests
for elem in self._get_elements(payload=payload_func(chunk, **kwargs)):
File "C:\Program Files\Python38\lib\site-packages\exchangelib\services\common.py", line 118, in _get_elements
for i in self._response_generator(payload=payload):
File "C:\Program Files\Python38\lib\site-packages\exchangelib\services\common.py", line 414, in _get_elements_in_response
container_or_exc = self._get_element_container(message=msg, name=self.element_container_name)
File "C:\Program Files\Python38\lib\site-packages\exchangelib\services\common.py", line 373, in _get_element_container
raise self._get_exception(code=response_code, text=msg_text, msg_xml=msg_xml)
exchangelib.errors.ErrorIrresolvableConflict: The send or update operation could not be performed because the change key passed in the request does not match the current change key for the item.