Outlook OAuth2 SMTP is not authenticating with mailkit

Hi!. I'm currently working on one specific app that should send e-mail via outlook and using mailkit. App is logging in via oauth2 and imap authentication is OK, but not smtp.

I'm getting this exception:

> 535: 5.7.3 Authentication unsuccessful

> [AM6P192CA0023.EURP192.PROD.OUTLOOK.COM]

Code sample:

    var publicClientApplication = PublicClientApplicationBuilder.Create(ClientId)
                        .WithRedirectUri(RedirectUri)
                        .Build();
    
    var scopes = new string[]
    {
        "email",
        "offline_access",
        "https://outlook.office.com/IMAP.AccessAsUser.All",
        "https://outlook.office.com/SMTP.Send"
    };
    
    var authToken = await publicClientApplication.AcquireTokenInteractive(scopes).ExecuteAsync();
    
    SaslMechanism oauth2;
    
    if (account.ImapClient.AuthenticationMechanisms.Contains("OAUTHBEARER"))
        oauth2 = new SaslMechanismOAuthBearer(authToken.Account.Username, authToken.AccessToken);
    else
        oauth2 = new SaslMechanismOAuth2(authToken.Account.Username, authToken.AccessToken);
    
    await SmtpClient.ConnectAsync(smtp.office365.com, 587, SecureSocketOptions.StartTls);
    await SmtpClient.AuthenticateAsync(oauth2);

API permissions in app registration:



Any ideas?

Hi Valentin I'm Anna and I'd be happy to help you with your question. In this Forum, we are Microsoft consumers just like yourself. Sorry for the trouble this might have caused. Please note that this is not my area of expertise but need to respond as the question was left unattended to. Please see this YouTube video and then retrace your steps https://www.youtube.com/watch?v=hOgvTDKKgnY

Note: This is a non-Microsoft website. The page seems to provide accurate and reliable information. Beware of advertisements on the site that may advertise products frequently classified as PUPs (potentially unwanted products). Do thorough research on any product advertised on the site before downloading and installing it. I hope this helps ;-), let me know if this is contrary to what you need, I would still be helpful to answer more of your questions. Best Regards, Anna Give back to the community. Help the next person with this problem by indicating whether this answer solved your problem. Click Yes or No at the bottom.

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated November 9, 2024 Views 5,365 Applies to: