-
-
Notifications
You must be signed in to change notification settings - Fork 373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[MimeKit.MimeMessage]::new() throws an error when using standard library in Net Framework 4.8 #683
Comments
ParserOptions is very simple, so I can't think of anything there that would cause problems. Normally, when a similar exception has occurred for MimeMessage .ctor, it's been related to charset related and it's been a TypeLoadException (or some such) thrown in the CharsetUtils static .ctor This is because not all character encodings (aka text encodings) are supported by default in .netstandard and it's necessary to pull in additional nugets for the non-unicode character encodings. Or at least that was the case with older .netstandard versions. I forget if that's an issue for 2.x. Do you think it might be possible to get more information, such as line numbers or something? I wonder if the ParserOptions error is just a side-effect of something else and is therefore a red-herring. |
No, no line numbers. However: [MimeKit.ParserOptions]::new() Gives:
If I try to follow down [MimeKit.Utils.CharsetUtils] Error:
When I check what options I have in MimeKit.Utils - only 3 options are there |
CharsetUtils is an internal class, it's not exposed in the public API. I suspect this may be where things are breaking in CharsetUtils.cs? #if NETSTANDARD |
I would say so. Any way this can be a workaround? Maybe I'm missing some NuGet? |
I want to say it depends on the System.Text.Encoding.CodePages nuget package, but I'm not 100% positive. |
I tried that, it didn't change anything :( |
I'm not sure what else you could try :( Best I can offer is maybe try adding some logging to the CharsetUtils static .ctor to see if that helps narrow things down. That class basically tries to load a bunch of widely-used text encodings and maps various aliases to them that are commonly used in mail headers. It tries to catch exceptions, but apparently some exceptions make it out. https://github.com/jstedfast/MimeKit/blob/master/MimeKit/Utils/CharsetUtils.cs#L41 |
I guess I have no other way - I'll try to play with it and add some debugging/try/catches all over the place to find what is actually throwing this. Till then I will use 2 separate sets of libraries. |
If you figure out where in the code an exception is being thrown, let me know and I will help you try to figure out a solution. |
Oh, I bet I know the issue... I think that Register method was meant to be called from the main program and not a library as part of any initialization. When that code runs in your .NET 4.8 environment, it throws because .NET 4.8 doesn't have that method - it's only supposed to be called from a .NET Core app. |
Is there anything I can do? |
No, I think the solution is for me to remove that line of code from MimeKit - if you want, can you test that theory for me? If that works, then the next step is for me to write docs and a FAQ about needing to call that method from .NET Core apps manually (since MimeKit won't do it anymore). Hopefully that won't break too many people, but I think if I'm right about this bug, then it'll be the Right Thing To Do(tm) since your use-case scenario should totally be expected to work. |
Yes, lets try to remove that line and see how it goes. Maybe this could be somehow detected that it's running as part of Net Framework or Net Core? |
Did some more digging here... It looks like the following code should work in .NET >= 4.6 according to the docs. Encoding.RegisterProvider (CodePagesEncodingProvider.Instance);
I re-read the comments above and I think I thought that this was being hit in .NET 4.8, but now that I just re-read again, it's .NET 4.5? Is that right? If so, then this is still likely the culprit. |
The minimal version for Net Standard 2.0 is 4.6.1 and this is what I "support". I expect PowerShell 5.1 users to at least install 4.6.1 net framework but it should work for 4.8 as well. I have 4.8 installed and it throws errors. |
Ah, interesting... |
This should be fixed in 2.14.0 |
Will test it out. Thank you ;) |
Hi |
You should take this into Mailozaurr repo and provide what exactly you're using, which version and when you get the error. This is probably not proper way to report an issue. |
Hi - This is probably caused by your Wrong Syntax. $message = New-Object MimeKit.MimeMessage You can type |
Regarding my issue: When creating a new MimeMessage via Through the Automatic $Error variable (Which for some reason now is empty, even after a reboot) I recursively doug down in the .Innerexceptions property and ran a .ToString() on each of them, to get the exception message.
I think this string is missing the System.Runtime assembly infos like Version and PublicKey but I had made sure before that they matched: What I also made sure is that the Project I'm working on sits locally, to circumvent issues (TypeLoadExceptions) i got earlier. At this point I've googled everything and doug deeply into it until my search results consistently only had about 2 entries. I've obtained |
There's few more DLLs that are required for things to just work: Default = 5.1, Core = PS7+ https://github.com/EvotecIT/Mailozaurr/tree/master/Lib you can see those files here. If you load them up it will work. |
Is this for Mailozaurr only or just to use MailKit successfully? |
Mailozaurr works with Mailkit/MimeKit. DNSClient, EmailValidation, MicrosoftIdentity* and Google* are not needed, but the rest of it pretty much yes |
Nope, still same results.
Sorry for it being german ^^' |
Why dont you use Mailozaur? |
II haven't looked enough into Mailozaur as a viable solution yet - My requirements are:
As far as I've looked, Mailozaurr is an incredibly complex solution and would probably run into the same error I have on my machine because it also uses Edit: I need to send emails. My GUIForm offers an RTF textbox, which I want to simply pass to MailKit. MailKit does support an RTF body, Mailozaurr does not. |
well have you tried installing Mailozaurr and seeing if you have that error? If not - you can just use "native" mailkit/mimekit with it as well. Just because the functions simplify things you don't want doesn't mean you have to use them. |
The dll that has been the cause of most of the reported TypeLoadExceptions over the past 6+ months has been System.Runtime.CompilerServices.Unsafe. MimeKit no longer event attempts to load System.Text.Encoding.CodePages anymore for similar reasons. Apparently there is/was a bug in the standard MSBuild targets that incorrectly resolved which version of that dll to bundle in the app directory if multiple nugets depended on different versions and also incorrectly populated the assembly mappings. Essentially, there seems to be no way for me to solve the issue without dropping the dependency completely. |
PowerShell, especially PowerShell 5.1 adds it's own problems with DLLs. In some cases you need to resolve to binary workaround such as 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. |
@PrzemyslawKlys Yea, I think in all of the cases so far where someone is getting a TypeLoadException with MimeKit now (typically the Unsafe.dll), it is usually because their project is also depending on another nuget which depends on a different version of the Unsafe.dll. With that in mind, I had bumped the Unsafe.dll dependency to 6.0.0 instead of using an old version, this way it would be easier to make everything work. Now users just need to edit their own app.config and create the mapping (or let VS do it). |
@PrzemyslawKlys I have tested Mailozaurr and it does (for some magical reason) not have any issues with MimeKit::new()... Edit: I had mismatched DLLs... sorry to bother! |
You could always add RTF support to mailozaurr - make a PR - it's 5 lines of code. It already supports attachments and has all the logic so it's as easy as adding small feature. If the server is fast it send an email in 250ms or so |
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:
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):
The text was updated successfully, but these errors were encountered: