我正在使用exchangelib连接到exchangelib 2013并回复电子邮件,但本周我的公司搬到了另一个城市。我的剧本在我们搬之前都很好。前景在新的地方对我来说很好
可能是一个知道如何解决这个问题的人。非常感谢
我的Python代码是:
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))当我尝试执行我的脚本时,我得到以下错误:
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--这是我启用日志后的输出:
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发布于 2022-04-20 09:27:54
服务器在初始NTLM握手后返回HTTP 500内部服务器错误。您可以尝试显式设置服务器版本,因为此错误发生在Exchange服务器版本协商期间,或者与Exchange管理员联系并让他们查看服务器日志。
发布于 2022-05-25 11:34:36
再次看一看,MalformedResponseError的原因是服务器用https://schemas.xmlsoap.org/soap/envelope/的名称空间发射XML元素。注意https。应该是http://schemas.xmlsoap.org/soap/envelope/。这可能是一个配置错误在您的服务器,因为我只有3次点击谷歌的https版本的网址。
您可以尝试更改exchangelib中的NS值,以查看它是否解决了您的问题:
import exchangelib.util
exchangelib.util.SOAPNS = "https://schemas.xmlsoap.org/soap/envelope/"https://stackoverflow.com/questions/71932852
复制相似问题