MimeKit: [MimeKit.MimeMessage]::new() throws an error when using standard library in Net Framework 4.8
Describe the bug I use your MimeKit/MailKit libraries in Mailozaurr PowerShell module. So far I’ve been using NET Standard libraries to support NET Core (PS 6, PS 7) and NET 4.5 library to support PowerShell 5.1. It worked but it was kind of double the amount of megs. So I tried to use NET STandard 2.0 in PowerShell 5.1 which should work just fine (because it’s supported) but it throws an error whenever I try to use:
[MimeKit.MimeMessage]::new()
Error:
Exception calling ".ctor" with "0" argument(s): "The type initializer for 'MimeKit.ParserOptions' threw an exception."
At C:\Support\GitHub\Mailozaurr\Public\Send-EmailMessage.ps1:440 char:5
+ $Message = [MimeKit.MimeMessage]::new()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : TypeInitializationException
Do you see any reason why would it throw an error like that? It works fine in PowerShell 6 and 7 so it would seem that MimeKit.ParserOptions could potentially have a code that only works in .NET Core but doesn’t work in .NET 4.6.1+
I can of course use different libraries if NET Framework is used, but it;'s just unnecessary space filler.
Platform (please complete the following information):
- OS: Windows 10
- .NET Runtime:
- .NET Framework: Net 4.6.2+ (I believe 4.8 or later is currently installed)
- MimeKit Version: 2.13 but I checked 2.11, and 2.11 - same thing
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 35 (12 by maintainers)
Commits related to this issue
- Use Reflection to call Encoding.RegisterProvider() This should fix issue #683 — committed to jstedfast/MimeKit by jstedfast 3 years ago
- Don't call Encoding.RegisterProvider() anymore Rely on developers calling this themselves. Fixes issue #816 Fixes issue #792 Fixes issue #785 Fixes issue #683 — committed to jstedfast/MimeKit by jstedfast 2 years ago
PowerShell, especially PowerShell 5.1 adds it’s own problems with DLLs. In some cases you need to resolve to binary workaround such as
https://github.com/EvotecIT/ImagePlayground/blob/ca2b14a24d182869be1e7654edaa779f72838aa9/Sources/ImagePlayground.PowerShell/OnImportAndRemove.cs#L1-L45
Othrewise I wasn’t able to get it even up and running. But so far for Mailozaurr and your libraries I don’t have problems anymore. I believe the problem is picking correct DLL version that works with what you are using, or build empty Visual Studio project that just is there to get you proper dependencies in place.
Mailozaurr works with Mailkit/MimeKit. DNSClient, EmailValidation, MicrosoftIdentity* and Google* are not needed, but the rest of it pretty much yes
Will test it out. Thank you 😉