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);