【问题标题】:How do I use exchangelib to connect to work email that uses Outlook Web App (OWA)?如何使用 exchangelib 连接到使用 Outlook Web App (OWA) 的工作电子邮件?
【发布时间】:2020-09-23 16:00:29
【问题描述】:

我目前正在尝试寻找通过一种语言连接到我的工作电子邮件的方法,python 似乎有一个我正在寻找的库。我找到了 exchangelib 库,但它有点旧,我找不到任何其他东西可以连接到 Exchange 服务器(我的工作电子邮件不是“@outlook.com”)。

我找到了我的用户名(“WORD\username”),并且我知道我的电子邮件、密码和 Outlook Web 应用电子邮件的 URL(“www.company.net”)

from exchangelib import Account, Configuration, Credentials, DELEGATE, Folder


def connect(SERVER, EMAIL, USERNAME, PASSWORD):
    """
    Get Exchange account cconnection with server
    """
    creds = Credentials(username=USERNAME, password=PASSWORD)
    config = Configuration(server=SERVER, credentials=creds)
    account = Account(primary_smtp_address=EMAIL,  config=config, autodiscover=False, access_type=DELEGATE)

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


def main():

    print(connect("www.company.net",
                  "my@email.net", "WORD\\username", "password"))

这只是为了打印我的电子邮件,但我似乎无法连接,因为主机没有回复

Failed to create cached protocol with key ('https://www.company.net/EWS/Exchange.asmx', Credentials('WORD\\username', '********')): HTTPSConnectionPool(host='www.company.net', port=443): Max retries exceeded with url: /EWS/Exchange.asmx (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x00000169BAF68FD0>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond'))

我尝试使用我的 OWA 电子邮件的完整 url 并改用 exchangelib service_endpoint 参数

def connect(SERVER, EMAIL, USERNAME, PASSWORD):
    """
    Get Exchange account cconnection with server
    """
    creds = Credentials(username=USERNAME, password=PASSWORD)
    config = Configuration(service_endpoint=SERVER, credentials=creds)
    account = Account(primary_smtp_address=EMAIL,  config=config, autodiscover=False, access_type=DELEGATE)

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

但获取授权类型失败

Failed to create cached protocol with key ('https://company.net/.../.../', Credentials('WORD\\username', '********')): Failed to get auth type from service

我有什么遗漏吗?可能是因为我公司的安全功能吗?我不确定如何正确执行此操作。

【问题讨论】:

    标签: python python-3.x email exchangewebservices exchangelib


    【解决方案1】:

    exchangelib 使用 EWS 连接到 Exchange 服务器。 OWA 只是一个基于 Web 的 Exchange 客户端。您无法以编程方式连接到它。不过,您的服务器完全有可能拥有可以连接到的 EWS 端点。

    您需要启用自动发现以自动查找 EWS 端点,或者向您的 Exchange 服务器管理员询问 EWS 的正确端点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-14
      • 1970-01-01
      • 2018-12-26
      • 1970-01-01
      • 2021-12-05
      • 1970-01-01
      • 2020-06-30
      • 2021-11-05
      相关资源
      最近更新 更多