Search The ForumSearch   RegisterRegister  LoginLogin

MailBee.NET IMAP

 AfterLogic Forum : MailBee.NET IMAP
Subject Topic: OAuth2 - Login to IMAP failed Post ReplyPost New Topic
Author
Message << Prev Topic | Next Topic >>
romanovi4
Newbie
Newbie


Joined: 13 April 2022
Location: Germany
Online Status: Offline
Posts: 2
Posted: 13 April 2022 at 1:13am | IP Logged Quote romanovi4

Hi,

i use MailBee.Net Imap to access the Emails on Exchange/Office365-Server. The login command throws an exception: MBN00000002 NO AUTHENTICATE failed.

My code:
string xoauthKey = OAuth2.GetXOAuthKeyStatic(null, authResult.AccessToken);
await tmpMailBeeImap.LoginAsync(email, xoauthKey, AuthenticationMethods.SaslOAuth2, AuthenticationOptions.None, null);

Log entries:
[10:05:14.70] [SEND] MBN00000001 CAPABILITY\r\n
[10:05:14.72] [RECV] * CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS ID UNSELECT CHILDREN IDLE NAMESPACE LITERAL+\r\n [Total 115 bytes received.]
[10:05:14.72] [RECV] MBN00000001 OK CAPABILITY completed.\r\n [Total 38 bytes received.]
[10:05:14.73] [INFO] Connected to mail service at host "outlook.office365.com" on port 993 and ready.
[10:05:14.74] [INFO] Will login as "".
[10:05:14.74] [INFO] Will try SASL XOAUTH2 authentication method.
[10:05:14.75] [SEND] MBN00000002 AUTHENTICATE XOAUTH2\r\n
[10:05:14.78] [RECV] + \r\n [Total 4 bytes received.]
[10:05:14.79] [SEND] dXNlcGRDMk9yMHFRbmcBAQ==\r\n
[10:05:16.24] [RECV] MBN00000002 NO AUTHENTICATE failed.\r\n [Total 37 bytes received.]
[10:05:16.32] [INFO] Error: The server has rejected authentication data sent by the client. The server responded: MBN00000002 NO AUTHENTICATE failed.

What is wrong in my code.

The Mailbee.EWS works with OAUth2 fine.
Back to Top View romanovi4's Profile Search for other posts by romanovi4
 
Alex
AfterLogic Support
AfterLogic Support
Avatar

Joined: 19 November 2003
Online Status: Offline
Posts: 2163
Posted: 13 April 2022 at 1:25am | IP Logged Quote Alex

Hi,

Access tokens can be specific to a particular protocol. EWS-compatible token may not work for IMAP or vice versa. And you'll need to have the corresponding permissions enabled in your OAuth2 app in Azure.

See OAuth 2.0 for Office 365 Accounts guide for details.

Regards,
Alex
Back to Top View Alex's Profile Search for other posts by Alex
 
romanovi4
Newbie
Newbie


Joined: 13 April 2022
Location: Germany
Online Status: Offline
Posts: 2
Posted: 13 April 2022 at 5:41am | IP Logged Quote romanovi4

Hi,

thank you for answer. The example works well in interactive way. But i need the implementation for a windows service without interaction with an user.

My code to get a access token:

var cca = ConfidentialClientApplicationBuilder
                      .Create(ClientId)
                      .WithClientSecret(ClientSecret)
                      .WithTenantId(tenantId)
                      .Build();

var ewsScopes = new string[] { "https://outlook.office365.com/.default" };
authResult = await cca.AcquireTokenForClient(ewsScopes).ExecuteAsync();

then imap connect and login.
Back to Top View romanovi4's Profile Search for other posts by romanovi4
 
Igor
AfterLogic Support
AfterLogic Support


Joined: 24 June 2008
Location: United States
Online Status: Offline
Posts: 5870
Posted: 13 April 2022 at 6:02am | IP Logged Quote Igor

Microsoft doesn't currently allow that:

Authenticate an IMAP, POP or SMTP connection using OAuth
Quote:
OAuth access to IMAP, POP, SMTP AUTH protocols via OAuth2 client credentials grant flow is not supported.


However, you should be able to use either EWS or Graph API for accessing mailbox, see the respective articles in MailBee.NET Objects Developer's blog:

Using MailBee.NET EWS to access Office 365 mailbox in non-interactive case
Using Graph API to access Office 365 mailbox in non-interactive case

--
Regards,
Igor, Afterlogic Support
Back to Top View Igor's Profile Search for other posts by Igor
 

If you wish to post a reply to this topic you must first login
If you are not already registered you must first register

  Post ReplyPost New Topic
Printable version Printable version

Forum Jump

Powered by Web Wiz Forums version 7.9
Copyright ©2001-2004 Web Wiz Guide