Outlook SMTP Oauth 发送 - 身份验证失败

标签 outlook azure-active-directory office365 mailkit smtp-auth

我一直在尝试使用 Legacy Mail API 在旧应用程序中实现电子邮件功能通过 Device Code Flow 获取 token 后添加 OAuth 支持从 Microsoft Sample Project 开始.

沿着这条路线,我添加了 SMTP.Send 和许多其他 API 权限来查找缺失的部分。 (包括{ "User.Read", "User.ReadBasic.All", "SMTP.Send", "offline_access", "Mail.Send"}怕漏掉一个)

我一直在使用 MailKit library 进行测试构建概念验证。

到目前为止,我有以下代码片段,在尝试进行身份验证后失败。

public void SendSmtpMessageAsync(string id, string accessToken)
{
    var message = new MimeMessage();
    message.From.Add(new MailboxAddress("From Name", "From <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c180a5a5b3a4b2b28184b9a0acb1ada4efa2aeac" rel="noreferrer noopener nofollow">[email protected]</a>"));
    message.To.Add(new MailboxAddress("To Name", "To <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b2f3d6d6c0d7c1c1f2f7cad3dfc2ded79cd1dddf" rel="noreferrer noopener nofollow">[email protected]</a>"));
    message.Subject = "How you doin'?";

    message.Body = new TextPart("plain")
    {
        Text = @"Test Email Content"
    };

    using (var client = new SmtpClient(new ProtocolLogger(Console.OpenStandardOutput())))
    {
        try
        {
            client.Connect("smtp.office365.com", 587, SecureSocketOptions.StartTls);

            var oauth2 = new SaslMechanismOAuth2(id, accessToken);

            var temp = client.AuthenticationMechanisms;
            client.Authenticate(oauth2);

            client.Send(message);
            client.Disconnect(true);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
    }
}


MailKit 日志

我已启用日志记录并获得了一个日志,该日志显示客户端正在连接,而不是发送 token 进行身份验证,但身份验证失败。

Connected to smtp://smtp.office365.com:587/?starttls=always
S: 220 MW3PR05CA0003.outlook.office365.com Microsoft ESMTP MAIL Service ready at Mon, 25 May 2020 21:31:07 +0000
C: EHLO [192.168.0.7]
S: 250-MW3PR05CA0003.outlook.office365.com Hello [<<My IP>>]
S: 250-SIZE 157286400
S: 250-PIPELINING
S: 250-DSN
S: 250-ENHANCEDSTATUSCODES
S: 250-STARTTLS
S: 250-8BITMIME
S: 250-BINARYMIME
S: 250-CHUNKING
S: 250 SMTPUTF8
C: STARTTLS
S: 220 2.0.0 SMTP server ready
C: EHLO [192.168.0.7]
S: 250-MW3PR05CA0003.outlook.office365.com Hello [<<My IP>>]
S: 250-SIZE 157286400
S: 250-PIPELINING
S: 250-DSN
S: 250-ENHANCEDSTATUSCODES
S: 250-AUTH LOGIN XOAUTH2
S: 250-8BITMIME
S: 250-BINARYMIME
S: 250-CHUNKING
S: 250 SMTPUTF8
C: AUTH XOAUTH2 <<Token omitted but I have confirmed that it is Base64 encoded and 
in the format of base64("user=" + userName + "^Aauth=Bearer " + accessToken + "^A^A")>>
S: 535 5.7.3 Authentication unsuccessful [MW3PR05CA0003.namprd05.prod.outlook.com]
MailKit.Security.AuthenticationException: 535: 5.7.3 Authentication unsuccessful 
[MW3PR05CA0003.namprd05.prod.outlook.com]

任何指导或资源将不胜感激,因为大多数现有帖子都是 2020 年之前添加的旧 SMTP 支持。此外,如果您发现任何误解,请告诉我,以便我可以做一些额外的阅读。

最佳答案

经过大量搜索并尝试与 Microsoft 交谈后,我找到了另一篇文章的答案。 (我添加了一个 POP3 调用,其格式与 OP 中的 SMTP 调用相同。) The answer据说包括范围 https://outlook.office.com/POP.AccessAsUser.All 所以在替换 SMTP 和 POP3 之前的 POP.AccessAsUser.All 之后调用使用现代身份验证进行。


此外,现在我知道了答案,我看到它记录在 Microsoft 的文档 Authenticate an IMAP, POP or SMTP connection using OAuth 中。验证这是否是正确的解决方案。

Make sure to specify the full scopes, including Outlook resource URLs, when authorizing your application and requesting an access token.

| Protocol  | Permission scope string
|-----------|-------------------------------------
| IMAP      | https://outlook.office.com/IMAP.AccessAsUser.All
| POP       | https://outlook.office.com/POP.AccessAsUser.All
| SMTP AUTH | https://outlook.office.com/SMTP.Send

希望这可以帮助其他面临同样问题的人

关于Outlook SMTP Oauth 发送 - 身份验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62027014/

相关文章:

Mac 版 Outlook 2011 : keyboard shortcut to accept meeting invite (respond without comments)

azure-active-directory - 事件网格订阅者的 AAD 身份验证

azure - 如何在客户端授权的情况下使用 Microsoft.Graph 并且不会收到 401

vba - 在 Outlook 中收到新邮件后如何触发宏运行?

c# - 如何检索 outlook 联系人的图片

vba - 有没有办法在撰写电子邮件时获取收件人联系人类别?

azure - 将用户/组和角色添加到 Azure AD native 应用程序

angularjs - 如何在 Id_token 中包含首选用户名声明

api - 如何从 Office 365 REST API 检索 ItemAttachment 内容?

ruby-on-rails - Rails actionmailer,gmail 有效,office 365 无效。