python的exchangelib 报错,我需要oauth的认证详解

公司升级了outlook MFA认证,

python的exchangelib 报错

from exchangelib import DELEGATE, Account, Credentials, Message, Mailbox, HTMLBody,FileAttachment,Configuration,NTLM
from exchangelib.protocol import BaseProtocol,NoVerifyHTTPAdapter
BaseProtocol.HTTP_ADAPTER_CLS = NoVerifyHTTPAdapter

def Email(to,cc_mail,subject, body, attachmentspath=None):
    
    creds = Credentials(
        username='xxx@bbbb.com',  # 账号
        password='**********'  # 密码
    )

    account = Account(
        primary_smtp_address='xxx@bbbb.com',  # 发送方邮箱地址
        credentials=creds,
        autodiscover=True,
        access_type=DELEGATE
    )
    m = Message(
        account=account,
        cc_recipients=cc_mail,
        subject=subject,
        body=HTMLBody(body),
        to_recipients=[Mailbox(email_address=to)]
    )

报错
raise AutoDiscoverFailed(
exchangelib.errors.AutoDiscoverFailed: All steps in the autodiscover protocol failed for email xxx@bbbb.com. If you think
this is an error, consider doing an official test at https://testconnectivity.microsoft.com

这个错误消息表明,在尝试自动发现 Exchange 服务器时失败了。通常,这种情况发生在以下几种情况:

你输入的电子邮件地址有误,无法找到对应的 Exchange 服务器。
你输入的电子邮件地址对应的 Exchange 服务器当前不可用。
你的程序无法连接到 Exchange 服务器,可能是因为网络问题或者防火墙设置。
如果你遇到了这种情况,建议你检查电子邮件地址是否正确,并检查网络连接是否正常。如果仍然无法解决问题,可以尝试使用官方测试工具(https://testconnectivity.microsoft.com)来检测 Exchange 服务器的连接情况。

# exchangelib.errors.AutoDiscoverFailed

from exchangelib import DELEGATE, Account, Credentials, Message, Mailbox, HTMLBody,Configuration,NTLM
from exchangelib.protocol import BaseProtocol,NoVerifyHTTPAdapter
BaseProtocol.HTTP_ADAPTER_CLS = NoVerifyHTTPAdapter

def Email(to, subject, body):
    creds = Credentials(
        username='xxx.com\exadmin',
        password='Spinfo0'
    )
    config = Configuration(server='xxx.com', credentials=creds, auth_type=NTLM)
    account = Account(
        primary_smtp_address='xxx@xxx.com',
        credentials=creds,
        autodiscover=False,
        access_type=DELEGATE,
        config=config
    )
    m = Message(
        account=account,
        subject=subject,
        body=HTMLBody(body),
        to_recipients = [Mailbox(email_address=to)]
    )
    m.send()

Email("xxx@xxx.com", "abc", "def")

我们用的也是outlook,python用的 exchangelib包对部分office 365兼容,你可以重置一下再看看。也可以测试一下这个版本下这个包到底能不能用。我们没有认证过这个东西,试试吧

# coding=utf-8
#
# Created on 2018/2/


from exchangelib import DELEGATE, Account, Credentials, Configuration, NTLM, Message, Mailbox, HTMLBody
from exchangelib.protocol import BaseProtocol, NoVerifyHTTPAdapter


#此句用来消除ssl证书错误,exchange使用自签证书需加上
BaseProtocol.HTTP_ADAPTER_CLS = NoVerifyHTTPAdapter


# 输入你的域账号如example\leo
cred = Credentials(r'EXAMPLE\leo', '输入你的密码')

config = Configuration(server='输入邮箱服务器网页地址', credentials=cred, auth_type=NTLM)
a = Account(
primary_smtp_address='输入你要绑定的邮箱名(leo@example.com)', config=config, autodiscover=False, access_type=DELEGATE
)

# 此处为用来发送html格式邮件的文件路径
with open(r'C:\Users\leo\Desktop\1.html') as f:
msg = f.read().decode('utf-8')

m = Message(
account=a,
folder=a.sent,
subject=u'测试邮件',
body=HTMLBody(msg),
to_recipients=[Mailbox(email_address='输入你要绑定的邮箱名(leo@example.com)')]
)
m.send_and_save()

【不知是否能帮助到你,辛苦】

望采纳


看起来是您使用的 exchangelib 库无法自动查找您的邮箱服务器的配置信息。您可以手动设置邮箱服务器的地址来避免这个问题:

creds = Credentials(
    username='xxx@bbbb.com',  # 账号
    password='**********'  # 密码
)

# 设置邮箱服务器的地址
config = Configuration(server='your.mail.server.com', credentials=creds)

account = Account(
    primary_smtp_address='xxx@bbbb.com',  # 发送方邮箱地址
    config=config,
    autodiscover=False,
    access_type=DELEGATE
)

代码没问题。大概是服务器地址变动了,需要设置一下。流程可以参考下面链接:
https://blog.csdn.net/zyself/article/details/120308716