【发布时间】:2018-03-13 11:55:17
【问题描述】:
我在 python 中使用 exchangelib 时遇到问题。我试试这个示例代码:
from exchangelib import DELEGATE, Account, Credentials
creds = Credentials(
username='xxxx\\username',
password="mypassword"
)
account = Account(
primary_smtp_address='surname.name@xxxx.fr',
credentials=creds,
autodiscover=True,
access_type=DELEGATE
)
# Print first 10 inbox messages in reverse order
for item in account.inbox.all().order_by('-datetime_received')[:10]:
print(item.subject, item.body, item.attachments)
我尝试了不同的用户名,但没有任何效果,而且我总是收到相同的错误消息:
AutoDiscoverFailed: All steps in the autodiscover protocol failed
顺便说一句,以防万一它有帮助,我尝试使用为 C# 编码的 Exchange Web 服务,它在这个凭据下工作得非常好,我可以发送邮件:
static void Main(string[] args)
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
// The last parameter is the domain name
service.Credentials = new WebCredentials("username", "password", "xxxx.lan");
service.AutodiscoverUrl("surname.name@xxxx.fr", RedirectionUrlValidationCallback);
EmailMessage email = new EmailMessage(service);
email.ToRecipients.Add("surname.name@xxxx.fr");
email.Subject = "salut ";
email.Body = new MessageBody("corps du message");
email.Send();
}
private static bool RedirectionUrlValidationCallback(string redirectionUrl)
{
// The default for the validation callback is to reject the URL.
bool result = false;
Uri redirectionUri = new Uri(redirectionUrl);
/* Validate the contents of the redirection URL. In this simple validation
callback, the redirection URL is considered valid if it is using HTTPS
to encrypt the authentication credentials. */
if (redirectionUri.Scheme == "https")
{
result = true;
}
return result;
}
提前致谢!
【问题讨论】:
-
自动发现协商过程中的某个地方 exchangelib 未能选择正确的服务器。不幸的是,自动发现非常复杂和脆弱,因此有很多事情可能会出错。您可以在 exchangelib 中打开 DEBUG info 并将输出中的步骤与来自 testconnectivity.microsoft.com 的报告进行比较,如果您发现它做错了什么,请随时针对 exchangelib 打开一个问题。
-
如果您透露了“xxxx.fr”背后的实际域名,您甚至可以找人为您调试。查找正确服务器的算法不需要凭据。
标签: python office365 exchange-server exchangewebservices exchangelib