jstedfast / MailKit

A cross-platform .NET library for IMAP, POP3, and SMTP.

Home Page:http://www.mimekit.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Authentication failed, when trying to connect to my mailbox.

ferreiralmpedrofm opened this issue · comments

Describe the bug
I am attempting to connect to my mail Inbox and retrieve my unread messages using the grand_type client_credentials, as this is a Windows service and the user has no involvement in the process.

I have successfully obtained the Token, however, I am unable to authenticate with it as I am receiving the message "Authentication failed."

Platform (please complete the following information):

  • OS: Windows
  • .NET Runtime: [e.g. CoreCLR, Mono]
  • .NET Framework: 4.7.2
  • MailKit Version: 4.0

Exception
If you got an exception, please include the exception Message and StackTrace.

Message "Authentication failed."

StackTrace " at FundManagerUtils.IMAP.d__13.MoveNext() in C:\FundManager\Source\Repos\FM\FundManagerUtils\Email\IMAP.cs:line 97\r\n at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.b__6_1(Object state)\r\n at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)\r\n at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)\r\n at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)\r\n at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()\r\n at System.Threading.ThreadPoolWorkQueue.Dispatch()\r\n at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()" string

Code Snippets
If applicable, add code snippets to help explain your problem.

// Add your code snippet here.
FormUrlEncodedContent content = new FormUrlEncodedContent(new List<KeyValuePair<string, string>>
            {
                    new KeyValuePair<string, string>("scope", "https://outlook.office365.com/.default"),
                    new KeyValuePair<string, string>("grant_type", "client_credentials"),
                    new KeyValuePair<string, string>("client_id", "client_id"),
                    new KeyValuePair<string, string>("client_secret", "client_secret"),
            });

            try
            {
                HttpClient client = new HttpClient();

                HttpResponseMessage response = await client.PostAsync(requestUri, content).ConfigureAwait(continueOnCapturedContext: false);
                string jsonString = await response.Content.ReadAsStringAsync();
                client.Dispose();
                if (!response.IsSuccessStatusCode)
                {
                    throw new Exception(jsonString);
                }


                JObject jobj = JObject.Parse(jsonString);
                JToken token = jobj["access_token"];


                return token.ToString();
            }
(...)

MailKit.Security.SaslMechanismOAuth2 authResult = new SaslMechanismOAuth2(email, token);

Client.Connect("outlook.office365.com", 993, true);
 Client.Authenticate(authResult); //Here I get the exception, always. 

https://ibb.co/drykhXk

Same issuse with me :(

This is a duplicate of this: #1126

Okay, thank you. I didn't notice that this issue had already been taken care of.