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

时间:2020-05-26 16:33:55

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

Legacy Mail API开始通过Device Code Flow获得令牌后,我一直在尝试使用Microsoft Sample Project在旧版应用程序中实现电子邮件功能,以添加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支持时。此外,如果您有任何误解,请告诉我,以便我进行其他阅读。

2 个答案:

答案 0 :(得分: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

希望这可以帮助遇到相同问题的其他人

答案 1 :(得分:0)

我有一个类似的问题,这是由于我对Microsoft 365的错误配置引起的。有关详细信息,请参见MailKit unsuccessful SMTP OAuth with Microsoft 365 server。希望这会有所帮助。