【问题标题】:Outlook SMTP Oauth Send - Authentication UnsuccessfulOutlook SMTP Oauth 发送 - 身份验证不成功
【发布时间】:2020-09-13 13:02:59
【问题描述】:

在通过Device Code FlowMicrosoft Sample Project 开头的令牌后,我一直在尝试使用Legacy Mail API 在旧版应用程序中实现电子邮件功能以添加OAuth 支持。

沿着这条路线,我添加了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 Address@Example.com"));
    message.To.Add(new MailboxAddress("To Name", "To Address@Example.com"));
    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 日志

我已启用日志记录并获得显示客户端连接而不是发送令牌进行身份验证但身份验证失败的日志。

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 支持时发布的。此外,如果您发现任何误解,请告诉我,以便我进行补充阅读。

【问题讨论】:

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


    【解决方案1】:

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


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

    确保在授权您的应用程序和请求访问令牌时指定完整的范围,包括 Outlook 资源 URL。

    | 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
    

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

    【讨论】:

      【解决方案2】:

      我遇到了类似的问题,这是由于我对 Microsoft 365 的错误配置造成的。有关详细信息,请参阅MailKit unsuccessful SMTP OAuth with Microsoft 365 server。希望这会有所帮助。

      【讨论】:

      • 感谢您提供额外的途径。不幸的是,添加域后我仍然遇到同样的错误。
      • !!!!这是解决方案!!!!!! mods keep delete my way 如何解决这个问题:打开 Microsoft 365 管理中心并转到用户 > 活动用户。选择用户,然后在出现的浮出控件中单击“邮件”。在电子邮件应用程序部分中,单击管理电子邮件应用程序。验证 Authenticated SMTP 设置:未选中 = 禁用,选中 = 启用。完成后,点击保存更改。
      猜你喜欢
      • 2023-02-08
      • 1970-01-01
      • 2016-04-10
      • 2019-01-26
      • 2010-10-27
      • 2015-04-29
      • 2018-01-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多