Sicos1977 / MSGReader

C# Outlook MSG file reader without the need for Outlook
http://sicos1977.github.io/MSGReader
MIT License
489 stars 168 forks source link

System.TypeInitializationException: The type initializer for 'MsgReader.Rtf.Font' threw an exception. #388

Closed OliverSteinau closed 9 months ago

OliverSteinau commented 9 months ago

the following code (on .NET 6.0):

using Storage.Message msg = new(strm);
var x = msg.BodyHtml();

throws the above TypeInitializationException (funnily enough, accessing msg.BodyRtf() doesn't...)

Stacktrace:

System.TypeInitializationException: The type initializer for 'MsgReader.Rtf.Font' threw an exception.
---> System.ArgumentException: 'Windows-1252' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method. (Parameter 'name')
at System.Text.EncodingTable.InternalGetCodePageFromName(String name)
at System.Text.EncodingTable.GetCodePageFromName(String name)
at System.Text.Encoding.GetEncoding(String name)
at MsgReader.Rtf.Font..cctor()
Sicos1977 commented 9 months ago

Did you register your encoding providers?

Sicos1977 commented 9 months ago

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

OliverSteinau commented 9 months ago

Why should I register encoding providers? I'm not using any encodings. And also, I'm trying to read "BodyHtml" which shouldn't need any.

The exception is thrown in line 195 in MSGReader/MsgReaderCore/Rtf/FontTable.cs:

public static Encoding DefaultEncoding { get; } = Encoding.GetEncoding("Windows-1252");

and I'm not accessing any Rtf stuff either. The complete code looks like this:

    private void Test () {
        using FileStream strm = new("d:/xx.msg", FileMode.Open, FileAccess.Read);
        using Storage.Message msg = new(strm);
        Debug.WriteLine(msg.BodyHtml);
    }
Sicos1977 commented 9 months ago

Because HTML is embedded into RTF with encoding and that is why you need encoding providers.

Sicos1977 commented 9 months ago

Normally msg reader should register the encoding providers when it detects that you are running on .net core... so please try to register it and see if it fixes your problem.

OliverSteinau commented 9 months ago

Because HTML is embedded into RTF with encoding

Funnily, when I access "msg.BodyRtf", the error does not occur...

And yes, after calling Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); no exception is thrown. As I said, this is on .NET 6

Sicos1977 commented 9 months ago

Because HTML is embedded into RTF with encoding

Funnily, when I access "msg.BodyRtf", the error does not occur...

And yes, after calling Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); no exception is thrown. As I said, this is on .NET 6

That is because you then get the RAW rtf and MSGReader does not try to get the HTML out of the RTF