我是在 C# 中使用 Mailkit 的新手,在使用 IMAP 连接到 Office365 企业帐户时遇到问题。我收到一条错误消息,指出身份验证失败。我喜欢创建一个应用程序来阅读电子邮件。使用普通的office365帐户,效果很好。没有适当的两因素身份验证,我应该使用应用程序密码。而且我不使用 Azure。
using MailKit.Net.Imap;
private void Connect(string host, int port, string email, string password)
{
ImapClient _client = new ImapClient();
_client.Connect(host, port, true);
_client.Authenticate(email, password);
_client.Disconnect(true);
}
“应用程序专用密码”与 2FA 无关,它们只是授予特定应用程序访问权限的另一种方式(通过为每个应用程序提供不同的密码)。这开始成为各种大型电子邮件服务的要求,因为除了基于网络的 UI 之外,它们越来越不接受您的真实密码。
如果您不适合使用应用程序专用密码,那么您可能需要按照设置 OAuth2.0 身份验证工作流程的说明进行操作:https://github.com/jstedfast/MailKit/blob/master /ExchangeOAuth2.md