ecederstrand / exchangelib

Python client for Microsoft Exchange Web Services (EWS)
BSD 2-Clause "Simplified" License
1.16k stars 249 forks source link

Issue with updating emails from mailbox: ErrorIrresolvableConflict #858

Closed EpsiophI closed 3 years ago

EpsiophI commented 3 years ago

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
ecederstrand commented 3 years ago

There’s nothing in exchangelib that would cause this, AFAIK. Google seems to suggest it’s either caused by other processes updating the same items simultaneously, or a bug in Exchange. See eg. https://stackoverflow.com/questions/38102586/errorirresolvableconflict-while-creating-an-event

Also, if you just need to edit the subject, you can improve performance if you just fetch the subject field (must be combined withe the update_fields attr):

for message in inbox.all().only('subject').order_by('-datetime_received')[:10]:
    message.subject = "Modified Subject"
    message.save(update_fields=['subject'])
ecederstrand commented 3 years ago

Closing this issue as I don't think it's a problem in exchangelib. Feel free to reopen if you think otherwise.