Sicos1977 / MSGReader

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

Unable to save .msg attachment from parent .msg file #425

Open paulkiernan opened 1 week ago

paulkiernan commented 1 week ago

Describe the bug Saving a .msg attachment from a parent .msg file fails.

To Reproduce Passing a .msg file into the following function that itself contains a .msg attachment produces the following error:

    public void ExtractAttachments(MsgReader.Outlook.Storage.Message message, string outputDirectory)
    {
        foreach (var attachment in message.Attachments)
        {
            if (attachment is MsgReader.Outlook.Storage.Attachment attach)
            {
                string filePath = Path.Combine(outputDirectory, attach.FileName);
                File.WriteAllBytes(filePath, attach.Data);
            }
            else if (attachment is MsgReader.Outlook.Storage.Message msg)
            {
                string msgFileName = msg.FileName;
                string msgFilePath = Path.Combine(outputDirectory, msgFileName);
                msg.Save(msgFilePath);
            }
        }
    }
Error processing message: srcOffset ('-64') must be a non-negative value. (Parameter 'srcOffset')
Actual value was -64.
   at System.ArgumentOutOfRangeException.ThrowNegative[T](T value, String paramName)
   at OpenMcdf.StreamView.Read(Byte[] buffer, Int32 offset, Int32 count)
   at OpenMcdf.CompoundFile.GetMiniSectorChain(Int32 secID)
   at OpenMcdf.CompoundFile.GetSectorChain(Int32 secID, SectorType chainType)
   at OpenMcdf.CompoundFile.GetData(CFStream cFStream)
   at OpenMcdf.CFStream.GetData()
   at MsgReader.Outlook.Storage.Message.<>c__DisplayClass153_0.<Copy>b__0(CFItem action)
   at OpenMcdf.CFStorage.<>c__DisplayClass13_0.<VisitEntries>b__0(IRBNode targetNode)
   at RedBlackTree.RBTree.DoVisitTreeNodes(Action`1 action, IRBNode walker)
   at RedBlackTree.RBTree.DoVisitTreeNodes(Action`1 action, IRBNode walker)
   at RedBlackTree.RBTree.DoVisitTreeNodes(Action`1 action, IRBNode walker)
   at RedBlackTree.RBTree.VisitTreeNodes(Action`1 action)
   at OpenMcdf.CFStorage.VisitEntries(Action`1 action, Boolean recursive)
   at MsgReader.Outlook.Storage.Message.Copy(CFStorage source, CFStorage destination)
   at MsgReader.Outlook.Storage.Message.<>c__DisplayClass153_0.<Copy>b__0(CFItem action)
   at OpenMcdf.CFStorage.<>c__DisplayClass13_0.<VisitEntries>b__0(IRBNode targetNode)
   at RedBlackTree.RBTree.DoVisitTreeNodes(Action`1 action, IRBNode walker)
   at RedBlackTree.RBTree.DoVisitTreeNodes(Action`1 action, IRBNode walker)
   at RedBlackTree.RBTree.DoVisitTreeNodes(Action`1 action, IRBNode walker)
   at RedBlackTree.RBTree.DoVisitTreeNodes(Action`1 action, IRBNode walker)
   at RedBlackTree.RBTree.DoVisitTreeNodes(Action`1 action, IRBNode walker)
   at RedBlackTree.RBTree.DoVisitTreeNodes(Action`1 action, IRBNode walker)
   at RedBlackTree.RBTree.VisitTreeNodes(Action`1 action)
   at OpenMcdf.CFStorage.VisitEntries(Action`1 action, Boolean recursive)
   at MsgReader.Outlook.Storage.Message.Copy(CFStorage source, CFStorage destination)
   at MsgReader.Outlook.Storage.Message.Save(Stream stream)
   at MsgReader.Outlook.Storage.Message.Save(String fileName)
   at NETMSGExtract.OutlookMessage.ExtractAttachments(String outputDirectory) in /source/OutlookMessage.cs:line 101
   at NETMSGExtract.MSGExtractor.ExtractMSG() in /source/MSGExtractor.cs:line 35
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

Expected behavior

The attached .msg file is written successfully to msgFilePath.

Screenshots N/A

Desktop (please complete the following information):

Smartphone (please complete the following information): N/A

Additional context N/A