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: 8
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: 2206
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: 8
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: 6038
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
 
romanovi4
Newbie
Newbie


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

Hello,

Microsoft supported now the client credentials flow for IMAP. Is this feature already supported in actually MailBee or should i wait for new release?

https://www.microsoft.com/en-us/microsoft-365/roadmap?filters=&searchterms=70577

Regards,
Roman
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: 2206
Posted: 09 August 2022 at 2:30am | IP Logged Quote Alex

Hello,

It's supported automatically because MailBee does not deal with generating tokens at all. It only consumes them. You create a token with a provider-specific API and feed it to MailBee. So if the provider-specific API now allows you to get the token in a new way, you can adapt your code which gets the token. The rest (when you pass this token to MailBee) stays the same.

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: 8
Posted: 31 August 2022 at 2:41am | IP Logged Quote romanovi4

Hello,

the authentication on Office365 via OAuth2 using the client credentials flow works.

But a MessageStatus-event does not come from the server when a new email arrives in the mailbox. Idling event is thrown regularly. Only when idling stopped then throws the MessageStatus-Events. As if event is being blocked by something....

What can it be?
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: 2206
Posted: 31 August 2022 at 3:23am | IP Logged Quote Alex

Hi,

Hard to say what could go wrong there but after some googling I can say you're not alone with IDLE problem there. I've seen reports it stopped working recently.

E.g. (last post in this topic says it stopped working this month):

https://stackoverflow.com/questions/73411037/imap-idle-is-not-returning-any-information-despite-new-emails-whilst-running

That's all I can say now.

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


Joined: 05 September 2022
Location: Netherlands
Online Status: Offline
Posts: 6
Posted: 06 September 2022 at 12:03am | IP Logged Quote phlexss

romanovi4 wrote:
Hello,

the authentication on Office365 via OAuth2 using the client credentials flow works.

But a MessageStatus-event does not come from the server when a new email arrives in the mailbox. Idling event is thrown regularly. Only when idling stopped then throws the MessageStatus-Events. As if event is being blocked by something....

What can it be?


How did you get credentials flow to work? What code do you use?
Back to Top View phlexss's Profile Search for other posts by phlexss
 
romanovi4
Newbie
Newbie


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

In Windows form application on button click i start the task and in the task i run the following code:

private void StartIdle()
{
     this.isActive = true;
     Task.Run(async () => {
          try
          {

               MailBee.Global.LicenseKey = MailBeeGlobalLicenseKey;
               this.mailBeeImapIdle = new MailBee.ImapMail.Imap();
               this.mailBeeImapIdle.Connected += MailBeeImapIdle_Connected;
               this.mailBeeImapIdle.Disconnected += MailBeeImapIdle_Disconnected;

               await this.mailBeeImapIdle.ConnectAsync("outlook.office365.com", 993);
               if (this.mailBeeImapIdle.GetExtension("IDLE") == null)
               {
                    return;
               }

               if (this.cca == null)
               {
                    this.cca = ConfidentialClientApplicationBuilder
                         .Create(this.txtApplicationID.Text)
                         .WithClientSecret(this.txtSecretKey.Text)
                         .WithTenantId(this.txtTenant.Text)
                         .Build();

               }
               string accessToken = this.GetToken();
               string xoauthKey = OAuth2.GetXOAuthKeyStatic(this.txtEmailAddress.Text, accessToken);
               await this.mailBeeImapIdle.LoginAsync(null, xoauthKey, AuthenticationMethods.SaslOAuth2, AuthenticationOptions.None, null);

               this.mailBeeImapIdle.SelectFolder("INBOX");
               this.mailBeeImapIdle.Idling += MailBeeImapIdle_Idling;
               this.mailBeeImapIdle.MessageStatus += MailBeeImapIdle_MessageStatus;

               while (this.isActive)
               {
                    MailBee.ImapMail.UidCollection uids = (MailBee.ImapMail.UidCollection)await this.mailBeeImapIdle.SearchAsync(true, "unseen", null);
                    if (uids.Count > 0)
                    {
                         MailBee.Mime.MailMessageCollection msgs = await this.mailBeeImapIdle.DownloadEntireMessagesAsync(uids.ToString(), true);
                         foreach (MailBee.Mime.MailMessage msg in msgs)
                         {
                              this.mailBeeImapIdle .SetMessageFlags(msg.UidOnServer.ToString(), true, MailBee.ImapMail.SystemMessageFlags.Seen, MailBee.ImapMail.MessageFlagAction.Add);
                         }
                    }
                    TimerStart();
                    await this.mailBeeImapIdle.IdleAsync();
               }
          }
          catch (Exception ex)
          {
               await this.StopIdleAsync();
          }
     });
}

private string GetToken()
{
     var authResult = this.cca.AcquireTokenForClient(ewsScopes).ExecuteAsync().Result;
     return authResult.AccessToken;
}
Back to Top View romanovi4's Profile Search for other posts by romanovi4
 

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