How to implement OAUTH 2.0 in Outlook using Ultimate Mail

0
Hi I'm trying to authenticate use OAUTH2 token based authentication for Outlook but not able to get authenticated. Whenever Authenticate is called using a token [**Authenticate(string Token, ImapAuthenticationMethod ImapAuthenticationMethod.OAuth2)**] it throws the following error ComponentPro.Net.Mail.ImapException: **AUTHENTICATE failed (NO).** at ComponentPro.Net.Mail.Imap.c_47d8efdd(String , ImapResponse , Boolean ) at ComponentPro.Net.Mail.Imap.c_56b07853(String ) at ComponentPro.Net.Mail.Imap.c_5e2380a4(String , String , ImapAuthenticationMethod , c_e ) at ComponentPro.Net.Mail.Imap.c_c224912a(String , String , ImapAuthenticationMethod ) at ComponentPro.Net.Mail.Imap.Authenticate(String token, ImapAuthenticationMethod method) I'm using the following code: try { var _task = Task.Run(() => { return GetAccessToken(); }); _task.Wait(); var tokenResult = _task.Result; AccessToken = tokenResult.AccessToken; MailClient = new Imap(); MailClient.Config = new ImapConfig() { AllowedTlsVersions = TlsSslVersion.TLS12 }; MailClient.Connect("outlook.office365.com", 993, SslSecurityMode.Implicit); string initAuthInfo = GenerateSASLToken(); MailClient.Authenticate(initAuthInfo, ImapAuthenticationMethod.OAuth20); if (MailClient.IsAuthenticated) ReadMailbox(); else throw new Exception($"User authentication failed."); } catch (Exception ex) { throw ex } finally { if (MailClient.IsConnected) MailClient.Disconnect(); } private async Task GetAccessToken() { try { var confidentialClientApplication = ConfidentialClientApplicationBuilder .Create(ClientId) .WithClientSecret(ClientSecret) .WithTenantId(TenantId) .Build(); var scopes = new string[] { "https://outlook.office365.com/.default" }; return await confidentialClientApplication.AcquireTokenForClient(scopes).ExecuteAsync(); } catch (Exception ex) { throw; } } private string GenerateSASLToken() { string authData = string.Format("user={0}{1}auth=Bearer {2}{1}{1}", UserName, '\x1', AccessToken); return Convert.ToBase64String(Encoding.ASCII.GetBytes(authData)); } If I use the same credential (SASL token) and call the **SendCommand** the response received is **OK AUTHENTICATE completed.** MailClient.SendCommand("AUTHENTICATE", new object[2] { "XOAUTH2", initAuthInfo }); ImapResponse _response = MailClient.ReadResponse(); Regards
 
asked 12/9/2022 8:53:06 AM
add a comment

1 Answers

0
Hi Your error suggests a scope issue . Check below post [NO AUTHENTICATE failed][1] This can solved by using correct scope suggested by Microsoft For Microsoft, we recommend https://outlook.office.com/IMAP.AccessAsUser.All or https://outlook.office.com/POP.AccessAsUser.All, and offline_access. Thank you for contacting us [1]: https://confluence.atlassian.com/jirakb/imap-setup-fails-with-authenticate-failed-error-in-logs-in-jira-server-185401609.html
edited 12/29/2022 11:28:05 AM
answered 12/29/2022 11:27:50 AM
  Hi Martin I'm using **ConfidentialClientApplicationBuilder** since the application is an unattended application and the only scope allowed in this scenario is "https://outlook.office365.com/.default". If I use the interactive mode using **PublicClientApplicationBuilder** the scopes "https://outlook.office.com/IMAP.AccessAsUser.All" or "https://outlook.office.com/POP.AccessAsUser.All", and "offline_access" are allowed. Please suggest how to authenticate in case of an **unattended application (daemon application)**. Alok Agrawal 1/4/2023 2:44:10 PM
add a comment

Your Answer

Not the answer you're looking for? Browse other questions tagged imap pop3 mail or ask your own question.