Python / exchangelib - 无法建立连接

时间:2017-06-26 16:14:37

标签: python python-3.x exchangewebservices exchangelib

抱歉我的英语不太好: 在Windows域中,我尝试使用pypi站点上给出的示例连接到我们的Exchangeserver(2010 / SP3):

from exchangelib import DELEGATE, NTLM, Account, Credentials, Configuration
import os, logging

logging.basicConfig(level=logging.DEBUG)

creds = Credentials(
    username='mydomain\\my.name', 
    password='mypassword')

config = Configuration(server='exchange.xxx.local/EWS/Exchange.asmx', credentials=creds)

account = Account(
    primary_smtp_address='my.name@mycompany.com',
    credentials=creds,
    autodiscover=False,
    access_type=DELEGATE)

for item in account.inbox.all().order_by('-datetime_received')[:30]:
    print(item.subject, item.body, item.attachments)

启动脚本后,我在python控制台中获得此输出:

DEBUG:exchangelib.protocol:Waiting for _protocol_cache_lock
DEBUG:exchangelib.protocol:Protocol __call__ cache miss. Adding key '('https://exchange.xxx.local/EWS/Exchange.asmx', Credentials('mydomain\\my.name', '********'), True)'
DEBUG:exchangelib.transport:Getting service auth type for https://exchange.xxx.local/EWS/Exchange.asmx
DEBUG:exchangelib.transport:Requesting b'<?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="Exchange2016" /></s:Header><s:Body><m:ResolveNames ReturnFullContactData="false"><m:UnresolvedEntry>mydomain\\my.name</m:UnresolvedEntry></m:ResolveNames></s:Body></s:Envelope>' from https://exchange.xxx.local/EWS/Exchange.asmx
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): exchange.xxx.local
Traceback (most recent call last):
  File "C:\CWS\Python-venv\p353-01\lib\site-packages\urllib3\connectionpool.py", line 594, in urlopen
self._prepare_proxy(conn)
  File "C:\CWS\Python-venv\p353-01\lib\site-packages\urllib3\connectionpool.py", line 810, in _prepare_proxy
conn.connect()
  File "C:\CWS\Python-venv\p353-01\lib\site-packages\urllib3\connection.py", line 294, in connect
self._tunnel()
  File "C:\seProg\python353\Lib\http\client.py", line 832, in _tunnel
message.strip()))
OSError: Tunnel connection failed: 407 Proxy Access Denied    
... (and many more lines with error messages)

我还尝试了来自exchangelib站点的其他选项,autodiscover = True,只有creds = given(没有config =)。没有成功。 然后我从https://ewseditor.codeplex.com/安装了EWS-Editor,以便了解通过EWS的连接是否可以在我公司的环境中工作。它可以正常使用该工具,只需要给它我的SMTP地址。

我非常感谢任何帮助。提前致谢!

1 个答案:

答案 0 :(得分:0)

您可能需要告诉exchangelib您的代理。您可以通过环境变量:https://github.com/ecederstrand/exchangelib/issues/123#issuecomment-295648189或通过自定义HTTPAdapter类来实现此目的:https://github.com/ecederstrand/exchangelib/issues/156#issuecomment-312391095