exchangelib - No Body element in SOAP response

844 Views Asked by At

I am using exchangelib to connect to Exchange Server 2013 and reply to emails, But this week my company moved to another City. All my Script was working fine before we move. Outlook working fine for me in the new location

May be someone know how to resolve this probleme. Thanks a lot

My Python code is :

from exchangelib import Account, Credentials, Configuration, Message, DELEGATE, Mailbox, FileAttachment, HTMLBody, FolderCollection
from exchangelib.protocol import BaseProtocol, NoVerifyHTTPAdapter
from exchangelib.folders import Folder
import warnings

warnings.filterwarnings("ignore")
BaseProtocol.HTTP_ADAPTER_CLS = NoVerifyHTTPAdapter

credentials = Credentials(username='DOMAIN\\USERNAME', password='XXXXXXXXX')

config = Configuration(server='XXXXXXXX', credentials=credentials)
account = Account(primary_smtp_address='ADRESSE_EMAIL', config=config, autodiscover=False, access_type=DELEGATE)

for email in account.inbox.all().order_by('-datetime_received')[:3]:
    print(str(email.subject))

When I trie to execute My script I get this errors :

Z:\AUTO_SCRIPTS>TEST.py
Account None: Exception in _get_elements: Traceback (most recent call last):
  File "C:\Users\AutoJob\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exchangelib\services\common.py", line 330, in _get_elements
    yield from self._response_generator(payload=payload)
  File "C:\Users\AutoJob\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exchangelib\services\common.py", line 292, in _response_generator
    response = self._get_response_xml(payload=payload)
  File "C:\Users\AutoJob\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exchangelib\services\common.py", line 421, in _get_response_xml
    header, body = self._get_soap_parts(response=r, **parse_opts)
  File "C:\Users\AutoJob\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exchangelib\services\common.py", line 518, in _get_soap_parts
    raise MalformedResponseError("No Body element in SOAP response")
exchangelib.errors.MalformedResponseError: No Body element in SOAP response

Traceback (most recent call last):
  File "Z:\AUTO_SCRIPTS\TEST.py", line 14, in <module>
    account = Account(primary_smtp_address='XXXXX_MYADRESSEEMAIL_XXXXXXX', config=config, autodiscover=False, access_type=DELEGATE)
  File "C:\Users\AutoJob\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exchangelib\account.py", line 204, in __init__
    self.version = self.protocol.version.copy()
  File "C:\Users\AutoJob\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exchangelib\protocol.py", line 492, in version
    self.config.version = Version.guess(self, api_version_hint=self._api_version_hint)
  File "C:\Users\AutoJob\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exchangelib\version.py", line 233, in guess
    list(ResolveNames(protocol=protocol).call(unresolved_entries=[name]))
  File "C:\Users\AutoJob\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exchangelib\services\common.py", line 252, in _elems_to_objs
    for elem in elems:
  File "C:\Users\AutoJob\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exchangelib\services\common.py", line 310, in _chunked_get_elements
    yield from self._get_elements(payload=payload_func(chunk, **kwargs))
  File "C:\Users\AutoJob\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exchangelib\services\common.py", line 330, in _get_elements
    yield from self._response_generator(payload=payload)
  File "C:\Users\AutoJob\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exchangelib\services\common.py", line 292, in _response_generator
    response = self._get_response_xml(payload=payload)
  File "C:\Users\AutoJob\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exchangelib\services\common.py", line 421, in _get_response_xml
    header, body = self._get_soap_parts(response=r, **parse_opts)
  File "C:\Users\AutoJob\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exchangelib\services\common.py", line 518, in _get_soap_parts
    raise MalformedResponseError("No Body element in SOAP response")
exchangelib.errors.MalformedResponseError: No Body element in SOAP response

This is the output after I enable logging :

Z:\AUTO_SCRIPTS>TEST.PY
WARNING:root:This will get logged to a file
DEBUG:exchangelib.protocol:Waiting for _protocol_cache_lock
DEBUG:exchangelib.protocol:Protocol __call__ cache miss. Adding key '('https://MyServer.CO/EWS/Exchange.asmx', Credentials('DOMAIN\\USERNAME', '********'))'
DEBUG:exchangelib.version:Asking server for version info using API version Exchange2019
DEBUG:exchangelib.services.common:Processing chunk 1 containing 1 items
DEBUG:exchangelib.services.common:Calling service ResolveNames
DEBUG:exchangelib.services.common:Trying API version Exchange2019
DEBUG:exchangelib.protocol:Server MyServer.CO: Increasing session pool size from 0 to 1
DEBUG:exchangelib.transport:Requesting b'<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<s:Envelope xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"><s:Header><t:RequestServerVersion Version="Exchange2019"/></s:Header><s:Body><m:ResolveNames ReturnFullContactData="false"><m:UnresolvedEntry>DOMAIN\\USERNAME</m:UnresolvedEntry></m:ResolveNames></s:Body></s:Envelope>' from https://MyServer.CO/EWS/Exchange.asmx
DEBUG:exchangelib.transport:Trying to get service auth type for https://MyServer.CO/EWS/Exchange.asmx
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): MyServer.CO:443
DEBUG:urllib3.connectionpool:https://MyServer.CO:443 "POST /EWS/Exchange.asmx HTTP/1.1" 401 0
DEBUG:exchangelib.transport:Request headers: {'User-Agent': 'exchangelib/4.7.2 (python-requests/2.21.0)', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Content-Type': 'text/xml; charset=utf-8', 'Content-Length': '460'}
DEBUG:exchangelib.transport:Response headers: {'request-id': '8bafd18d-0fe8-46be-b06f-56da0d19f49e', 'X-OWA-Version': '15.1.2176.14', 'WWW-Authenticate': 'Negotiate, NTLM', 'X-FEServer': 'DCSMSG02', 'Date': 'Tue, 19 Apr 2022 23:12:48 GMT', 'Content-Length': '0', 'Set-Cookie': 'TS010bba1d=01c63b1ddfb6be746dd7856a2fb8b268acf7e960c087d44fd6a6167e4a122947e9a18669aaa1727cad6495b9fb5b9338897d30a80e; Path=/'}
DEBUG:exchangelib.transport:Auth type is NTLM
DEBUG:exchangelib.protocol:Server MyServer.CO: Created session 29156
DEBUG:exchangelib.protocol:Server MyServer.CO: Waiting for session
DEBUG:exchangelib.protocol:Server MyServer.CO: Got session 29156
DEBUG:exchangelib.util:Session 29156 thread 6584: retry 0 timeout 120 POST'ing to https://MyServer.CO/EWS/Exchange.asmx after 10s wait
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): MyServer.CO:443
DEBUG:urllib3.connectionpool:https://MyServer.CO:443 "POST /EWS/Exchange.asmx HTTP/1.1" 401 0
DEBUG:urllib3.connectionpool:https://MyServer.CO:443 "POST /EWS/Exchange.asmx HTTP/1.1" 401 0
DEBUG:urllib3.connectionpool:https://MyServer.CO:443 "POST /EWS/Exchange.asmx HTTP/1.1" 500 None
DEBUG:exchangelib.util:Retry: 0
Waited: 10
Timeout: 120
Session: 29156
Thread: 6584
Auth type: <requests_ntlm.requests_ntlm.HttpNtlmAuth object at 0x041EDFD0>
URL: https://MyServer.CO/EWS/Exchange.asmx
HTTP adapter: <exchangelib.protocol.NoVerifyHTTPAdapter object at 0x041EDF70>
Allow redirects: False
Streaming: False
Response time: 0.15700000000651926
Status code: 500
Request headers: {'User-Agent': 'exchangelib/4.7.2 (python-requests/2.21.0)', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'Keep-Alive', 'Content-Type': 'text/xml; charset=utf-8', 'Content-Length': '460', 'Authorization': 'NTLM TlRMTVNTUAADAAAAGAAYAGgAAAD2APYAgAAAAAcABwBYAAAACQAJAF8AAAAAAAAAaAAAABAAEAB2AQAANoKJ4gYBsR0AAAAPyNMB8KYPBxfU0wqCe2C4k0JBUklEQURlZGl0aXF1ZW4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABR8LOgphZIm/EAeDYtJE50AQEAAAAAAACmmnv8QlTYAVM52uSDOhWSAAAAAAIADgBCAEEAUgBJAEQAQQBEAAEAEABEAEMAUwBNAFMARwAwADIABAAiAGIAYQByAGkAZABhAGQALgBiAGEAbQBuAGUAdAAuAG0AYQADADQARABDAFMATQBTAEcAMAAyAC4AYgBhAHIAaQBkAGEAZAAuAGIAYQBtAG4AZQB0AC4AbQBhAAUAEgBiAGEAbQBuAGUAdAAuAG0AYQAHAAgAppp7/EJU2AEGAAQAAgAAAAoAEACeNHLkFPXbs0xQaxoIbUbvAAAAAAAAAAA9VKKIFFe6RMuugXH+iNwp', 'Cookie': 'TS010bba1d=01c63b1ddf2aa69892928ea66f2646b2e49bf49a9833e65a8fb482f25bddf77c5a77f13c5229273fc883601143b57fecd64088296c; Path=/'}
Response headers: {'Cache-Control': 'private', 'Content-Type': 'text/xml; charset=utf-8', 'request-id': 'c1730b03-7fd2-4719-8566-bafabc9675e9', 'X-CalculatedBETarget': 'dcsmsg02.DOMAIN.co', 'X-DiagInfo': 'DCSMSG02', 'X-BEServer': 'DCSMSG02', 'Set-Cookie': 'exchangecookie=82163548ce1145e5bb345d3641a2f2fe; expires=Wed, 19-Apr-2023 23:12:48 GMT; path=/; HttpOnly, X-BackEndCookie=S-1-5-21-1454471165-1292428093-839522115-21152742=u56Lnp2ejJqByMzPyc+cnprSzsvNntLLz83O0sfPysfSxpzOyZnHzZnMz53IgYHNz83N0s/K0s7Gq83Mxc7NxcvHgZ2ekpGai9GSnoHP; expires=Thu, 19-May-2022 23:12:48 GMT; path=/EWS; secure; HttpOnly, TS010bba1d=01c63b1ddf2aa69892928ea66f2646b2e49bf49a9833e65a8fb482f25bddf77c5a77f13c5229273fc883601143b57fecd64088296c; Path=/, TS01aa8c4e=01c63b1ddf2aa69892928ea66f2646b2e49bf49a9833e65a8fb482f25bddf77c5a77f13c5229273fc883601143b57fecd64088296c; path=/EWS', 'Persistent-Auth': 'true', 'X-FEServer': 'DCSMSG02', 'Date': 'Tue, 19 Apr 2022 23:12:48 GMT', 'Transfer-Encoding': 'chunked'}
DEBUG:exchangelib.util.xml:Request XML: <?xml version='1.0' encoding='utf-8'?>
<s:Envelope
    xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"
    xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
  <s:Header>
    <t:RequestServerVersion Version="Exchange2019"/>
  </s:Header>
  <s:Body>
    <m:ResolveNames ReturnFullContactData="false">
      <m:UnresolvedEntry>DOMAIN\USERNAME</m:UnresolvedEntry>
    </m:ResolveNames>
  </s:Body>
</s:Envelope>
Response XML: <?xml version='1.0' encoding='utf-8'?>
<s:Envelope
    xmlns:s="https://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
    <s:Fault>
      <faultcode
    xmlns:a="https://schemas.microsoft.com/exchange/services/2006/types">a:ErrorInvalidRequest</faultcode>
      <faultstring xml:lang="en-US">The request is invalid.</faultstring>
      <detail>
        <e:ResponseCode
    xmlns:e="https://schemas.microsoft.com/exchange/services/2006/errors">ErrorInvalidRequest</e:ResponseCode>
        <e:Message
    xmlns:e="https://schemas.microsoft.com/exchange/services/2006/errors">The request is invalid.</e:Message>
      </detail>
    </s:Fault>
  </s:Body>
</s:Envelope>
DEBUG:exchangelib.protocol:No retry: no fail-fast policy
DEBUG:exchangelib.util:Got status code 500 but trying to parse content anyway
DEBUG:exchangelib.util:Session 29156 thread 6584: Useful response from https://MyServer.CO/EWS/Exchange.asmx
DEBUG:exchangelib.protocol:Server MyServer.CO: Releasing session 29156
DEBUG:exchangelib.services.common:No header in XML response
WARNING:exchangelib.services.common:Account None: Exception in _get_elements: Traceback (most recent call last):
  File "C:\Users\AutoJob\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exchangelib\services\common.py", line 330, in _get_elements
    yield from self._response_generator(payload=payload)
  File "C:\Users\AutoJob\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exchangelib\services\common.py", line 292, in _response_generator
    response = self._get_response_xml(payload=payload)
  File "C:\Users\AutoJob\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exchangelib\services\common.py", line 421, in _get_response_xml
    header, body = self._get_soap_parts(response=r, **parse_opts)
  File "C:\Users\AutoJob\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exchangelib\services\common.py", line 518, in _get_soap_parts
    raise MalformedResponseError("No Body element in SOAP response")
exchangelib.errors.MalformedResponseError: No Body element in SOAP response
2

There are 2 best solutions below

1
On

The server is returning an HTTP 500 Internal Server Error after the initial NTLM handshake. You can try to set the server version explicitly since the error happens during Exchange server version negotiation, or contact your Exchange admins and have them look in the server logs.

0
On

Having a look at this again, the reason for the MalformedResponseError is that the server emits XML SOAP elements with a namespace of https://schemas.xmlsoap.org/soap/envelope/. Notice the https. It should be http://schemas.xmlsoap.org/soap/envelope/. This may be a configuration error on your server since I only get 3 hits on Google for the https version of the URL.

You can try changing the NS value in exchangelib to see if it fixes your issue:

import exchangelib.util

exchangelib.util.SOAPNS = "https://schemas.xmlsoap.org/soap/envelope/"