First, a little explanation of why I have 2 issues. This issue is related to v3.3.0 of MimeKit, whereas the other issue arises when I update MimeKit to v3.4.0.
As I encountered the issue regarding v3.4.0 I rolled back to v3.3.0 to get it into my test environment, but now a different issue came up.
The Application is a Service Library running on IIS.
My Dev environment is running Win 10, Visual Studio 2022 x64 using C#.
My test environment is Windows Server 2019, running IIS 10.0 (17763.1).
When i use MimeKit 3.3.0 to create a new MimeMessage the MimeMessage result = new MimeMessage();
throws an exception:
The type initializer for 'MimeKit.ParserOptions' threw an exception. With a further InnerException: The type initializer for 'MimeKit.Utils.CharsetUtils' threw an exception.
What I've tried:
- Added
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
. Effect: NONE - Set Allow unsafe code on all Configurations in my project settings. Effect: NONE
- Checked that the
System.Text.Encoding.CodePages
is v6.0.0: it is.
Made a Windows Form App and put it in the same folder as the Service Library so I know it's using the same DLLs. That works fine and sends the email.
I am out of ideas, and I am starting to think there might be an issue with it from a Service Library, although it works fine in my Dev Env; then again it's Visual Studio that starts a WcfSvcHost
and not using IIS there, so the issue might be permission related, but I have no idea where to start as everything else in the Service Library is running - even other DLLs are working.
Edit 1:
Well, I did update both MimeKit and MailKit to v 3.4.0 for a start
Dev Env. Everything runs as it should, no errors and mail just goes through, as it should.
Test Env. For the Service Library, everything runs up until the point i Create MimeMessage, BUT since MailKit 3.3.0 and MimeKit 3.4.0 the error from the test env. has changed, and now the problem lies with the following Exception:
The type initializer for 'PerTypeValues`1' threw an exception. Inner Message: Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
I've checked the version on System.Runtime.CompilerServices.Unsafe and it's 6.0.0, so why it's reffering to 4.0.4.1 i do not understand. As for my Windows Forms Test App, everthing runs like in the dev. env. no errors, the mail just goes through.
The thing about the Windows Forms Test App and The Service Library itself is that I have placed both in the same folder, which means that both apps use the same DLLs on the same machine to run the two different apps, what i am thinking is that some code in the Win Forms App is being run that is not being run when the app is a Service Library, as far as i can see the main differences in the USINGs is that the Win Form App uses System.Windows.Forms, where the Service Library uses System.ServiceModel. Could there be a #IF that is ignored when using System.ServiceModel?
According to the Package Dependencies (https://www.nuget.org/packages/MimeKit) on MimeKit, .NET 2.0 and .NET 2.1 requires System.Text.Encoding.CodePages >= 6.0.0, but apparently is not required for .NETFramework 4.x, that was the reason i updated both to 3.4.0. But i now have to ask, is that list wrong since you now tell me that .NETFramework requires System.Text.Encoding.CodePages = 4.5.1
Well, that was that test, now the objective is to remove all NuGet packages that is used by MimeKit and MailKit, and roll everything back once again, this time, i am going much much harder to it, removing ALL NuGet Packages and starting over, hoping that my code does not break from that manouver.
Will write back a bit later to give the results of that trial attempt to get things running.
Regards Frank
Edit 2:
Well, that was no fun removing all the NuGet Packages, but finally i did it, and then installed all the required packs again, now MimeKit is 3.3.0, MailKit is 3.3.0, System. Text.Encoding.CodePages is 4.5.1
Now the Dev Env. is NOT running.
Below message from VS is not in the least helping me.
There was a conflict between
"System.Text.Encoding.CodePages, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" and
"System.Text.Encoding.CodePages, Version=4.5.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
"System.Text.Encoding.CodePages, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" was chosen because it was primary and
"System.Text.Encoding.CodePages, Version=4.5.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" was not.
References which depend on "System.Text.Encoding.CodePages, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
[***\Development\Visual Studio\ac\AppSQL\packages\System.Text.Encoding.CodePages.4.5.1\lib\net461\System.Text.Encoding.CodePages.dll].
***\Development\Visual Studio\ac\AppSQL\packages\System.Text.Encoding.CodePages.4.5.1\lib\net461\System.Text.Encoding.CodePages.dll
Project file item includes which caused reference "***\Development\Visual Studio\ac\AppSQL\packages\System.Text.Encoding.CodePages.4.5.1\lib\net461\System.Text.Encoding.CodePages.dll".
System.Text.Encoding.CodePages, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL
References which depend on "System.Text.Encoding.CodePages, Version=4.5.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" [].
***\Development\Visual Studio\ac\AppSQL\packages\MimeKit.3.3.0\lib\net48\MimeKit.dll
Project file item includes which caused reference "***\Development\Visual Studio\ac\AppSQL\packages\MimeKit.3.3.0\lib\net48\MimeKit.dll".
MimeKit, Version=3.3.0.0, Culture=neutral, PublicKeyToken=bede1c8a46c66814, processorArchitecture=MSIL
MailKit, Version=3.3.0.0, Culture=neutral, PublicKeyToken=4e064fe7c44a8f1b, processorArchitecture=MSIL acAppSQLServerLibrary
When i look at the DLL files referenced, i CANNOT find any other file than the one sporting version 4.6.2 (That version number is found in the DLL file itself) and i cannot see anywhere 4.1.1.0 is mentioned neither in my code, and cannot be found anywhere as a text in any of my included files. Where does C# get version 4.1.1.0 from, as far as i can see in the NuGet versions, a version 4.1.1.0 has never ever existed, it jumped in version # from 4.0.1. to 4.3.0, with nothing in between.
My breakdown of the message is as follows: It tells me the file: ***\Development\Visual Studio\ac\AppSQL\packages\System.Text.Encoding.CodePages.4.5.1\lib\net461\System.Text.Encoding.CodePages.dll is referencing "System.Text.Encoding.CodePages, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" but when i look at that file i only see 4.5.1.0 references but none for 4.1.1.0 and the DLL file version is still 4.6.2
To me this make no sense, how can a file in a folder called "packages\System.Text.Encoding.CodePages.4.5.1\lib\net461\System.Text.Encoding.CodePages.dll" reference itself with a version that has never existed, am i missing something here.
Kind regards Frank
Edit 3:
I see I forgot to mention that I am using .NETFramework 4.8 for the Service Library and the Windows Form Test App. Is there any other versions I have forgotten to mention?
Kind regards Frank