Sending Mail Using OAuth 2.0: The Handshake Failed Due to an Unexpected Packet Format

Hi @Andrey_Potapov
Following are my server details do let me know if you need any other information in addition to the below one:-

Windows Server 2016 Datacenter
Protocols enabled are
TLS - 1.0,1.1,1.2
SSL - 2.0,3.0
PCT 1.0

All Ciphers,hashes are enabled

.net framework version being used is 4.7.2
aspose version being used is 21.4

@anmolsdna,
Thank you for the additional information.

We are still waiting for the standalone comprehensive code example using OAuth 2.0 from you that can be used for reproducing the error.

@Andrey_Potapov
Sharing you sample code… I am using dll of this project in my web application…So providing you the same…

You need to debug the test case with the name SendMailsWithMicrosoftOAuth in class SmtpClientTest.cs

As already shared the security related code is in SmtpMailClient.cs line number 128 in which security options are being set…

I have already shared this security code snippet earlier. As said the same code works fine on local machine but not on windows server 2016 datacenter. So it is more of a security issue…rest you can have a look to the code…

@anmolsdna,
Thank you for the code sample. I passed it to our development team.

@Andrey_Potapov any update on this one. As we are stuck in completing the development

@anmolsdna,
I requested an estimated time to fix this issue from our development team. I will let you know as soon as possible.

@anmolsdna,
Our development team will investigate the issue this week. If a bug is found, it will be fixed in the next release.

@anmolsdna,
Unfortunately, our development team has not managed to reproduce the error in the same environment (Windows Server 2016 Datacenter).
Please try to do the following:

  1. Check the issue with the latest version of Aspose.Email.
  2. Enable logging, start the application with the error, and send us a log file.
  3. Set SecurityOptions.SSLExplicit for the client, start the application with logging and if the error occurs in this case, send us the log file.

I will do the needful and will revert

@Andrey_Potapov

I added app.config but the log file is not getting generated.
You can use the same sample code just upgrade it to 21.5, Set the below setting in SmtpMailClient.cs file
SecurityOptions.SSLExplicit //line number 148.

Rest will update you…

@anmolsdna,
I passed your information to our development team. I will inform you of any progress.

@anmolsdna,
Did you try to do 1 and 3 actions without logging on your side?

@Andrey_Potapov
Yes, following things I did at my side once you shared the 3 steps

  1. Did all the 3 steps in code sample
    OUTPUT: smtp unit test case works fine, but after giving wrong credentials or settings no logs generated
  2. Same modification done in my dll project.
    OUTPUT: smtp unit test case works fine, but after giving wrong credentials or settings no logs generated
  3. Give reference of my dll project to my .net application after doing the 3 steps
    OUTPUT
    1. On local as well as on server machine I get the below error in catch, but no log generated

An error occured while connecting SmtpClient: AsposeException: Could not load file or assembly ‘Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed’ or one of its dependencies. The system cannot find the file specified.—> System.IO.FileNotFoundException: Could not load file or assembly ‘Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed’ or one of its dependencies. The system cannot find the file specified. File name: 'Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed’at IdentityModel.Client.ProtocolResponse.d__01.MoveNext()at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1.Start[TStateMachine](TStateMachine& stateMachine)at IdentityModel.Client.ProtocolResponse.FromHttpResponseAsync[T](HttpResponseMessage httpResponse, Object initializationData)at IdentityModel.Client.HttpClientTokenRequestExtensions.d__8.MoveNext() — End of stack trace from previous location where exception was thrown — at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)at IdentityModel.Client.HttpClientTokenRequestExtensions.d__2.MoveNext() — End of stack trace from previous location where exception was thrown — at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult()at Onguard.Email.Exchange.AzureTokenProvider.<GetAccessTokenAsync>d__11.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult()at Onguard.Email.Exchange.AzureTokenProvider.GetAccessToken(Boolean ignoreExistingToken)at #=zymdEgzfzjZx9qeCvDAGNgbsE0JoZj9zj2w==.#=ztt_1rJA=(String #=z3Qmxo0c=, ITokenProvider #=zkCWRhMw=)at #=zymdEgzfzjZx9qeCvDAGNgbsE0JoZj9zj2w==.#=zM3izurY=(String #=zW92_sds=, NetworkCredential #=zmyV$tpY=, Object #=zAorLCsaG547s)at #=zebNWMvvliJnbtk7Zm3EtgHuv4jVlommmI_MDj0k=.#=zof9xNp0hieUR(#=zK30BhX_PFQiGJGTPi7hs1D$9AIHJ3$l5RA== #=zkDPz_vQ=, NetworkCredential #=zNNlOd$I=)at #=zebNWMvvliJnbtk7Zm3EtgHuv4jVlommmI_MDj0k=.#=zOlH1ecdwtIiAmDaUlCnqOEs=(String #=zhZHrju8=, Int32 #=znxd$uNI=)at #=zebNWMvvliJnbtk7Zm3EtgHuv4jVlommmI_MDj0k=.#=zGZLwRJDNFos7(String #=zhZHrju8=, Int32 #=znxd$uNI=, SecurityOptions #=zq0B2vLhmyob5)at #=zebNWMvvliJnbtk7Zm3EtgHuv4jVlommmI_MDj0k=.#=z8u8H8yH2t29F(String #=zhZHrju8=, Int32 #=znxd$uNI=, IList`1 #=zP29groQzTII7) WRN: Assembly binding logging is turned OFF. To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1. Note: There is some performance penalty associated with assembly bind failure logging. To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog]. — End of inner exception stack trace —at #=zirNI$9FSMqK4aNokyUKKMxcZXW1c.#=zsutwAyw=(IAsyncResult #=zX09FF5g=)at #=zirNI$9FSMqK4aNokyUKKMxcZXW1c.#=zA7pbz20=()at #=zj0Jow2BXKanIAGAs4VvDFyq5lVbYVVZ8yJW1sho=.#=z$mnlql0=(#=zxkLbMQAqxHJE1UZtXp_OHaP4IuVeQVgNfA== #=zgjePO0Q=)at #=zu$FnsJBaomaGGgGs$h2dPwP9seOK.#=zTvT7iLyTkTZY(Int32 #=zprRLj1A=, #=zxkLbMQAqxHJE1UZtXp_OHaP4IuVeQVgNfA== #=zDAqc4xw=)at #=zu$FnsJBaomaGGgGs$h2dPwP9seOK.#=z5XHQvNs=(Boolean #=zOgGwN1pLkKsv, #=zxkLbMQAqxHJE1UZtXp_OHaP4IuVeQVgNfA== #=zDAqc4xw=)at Aspose.Email.Clients.EmailClient.CreateConnection(Boolean createAsDefaultConnection)at Onguard.Email.Clients.SmtpMailClient.ConnectAsync() 06/18/2021 20:02:26 Error : Connection can not be established for setting UUID 123.

@anmolsdna,
Did I understand correctly that after updating Aspose.Email to the latest version, enabling logging and applying SecurityOptions.SSLExplicit, you get the AsposeException?

@Andrey_Potapov
Yes. In DLL project alone it works fine(unit test case) as mentioned in my previous comment.But, after using this DLL project reference in my .net web application. It gives the AsposeException in both local as well as on server.
It would be great if you can let me know that why log file is not getting generated. Let me know If I miss anything from the below steps for generating the aspose log file.

  1. Added the app.config file in my DLL project.
  2. Changed the security option to SecurityOptions.SSLExplicit
  3. Updated Aspose.Email to version 21.5

Note: Its the DLL project in which I have used Aspose.

@anmolsdna,

Could you please share a simple standalone .NET web application reproducing the error?

OK I will see what i can do.

The tool I am using is Logi Analytics where we have a feature(Plugins) of creating DLL using c# code…Whose name we can use in Logi Analytics reports. So my project structure in image

  1. DLL project named MyEmailServer Containing Aspose (Totally seperate entity not part of solution). A nuget package created.
  2. MyEmailServer DLL being consumed as nuget package in another DLL project named MailUtility that is part of same solution containing other projects also
  3. The project reference of the MailFactory dll created in 2nd step is being used in Logi Analytics DLL project feature part of same solution. So here I am getting error

NOTE: The MailFactory dll created in 2nd step is being used in other projects of the solution and is working totally fine for Microsoft

image.png (9.2 KB)

@anmolsdna,
Thank you for the additional information. Unfortunately, we need some project reproducing the error on our side. Please try to isolate the issue and share such a project if you find the issue with Aspose.Email API.

@Andrey_Potapov
we were able to resolve the issue. The issue was related to a json dll version which was higher in one project. once the version were in sync the error got resolved… Thanks for your valuable feedback.

@anmolsdna,
We are glad that you have fixed the error on your side.