Free Support Forum - aspose.com

I couldn't extract some messages in OST file

When I extract messages in “synchronization problems folder” in an OST file
EndOfStreamException occurs
I tried “folderInfo.EnumerateMapiMessages()” , “folderInfo.GetContents()” and “folderInfo.EnumerateMessages()” but they didn’t work.

What do you think is the cause?

@HM_Company,

I have observed the issue shared by you and request you to please share the sample OST file along with code snippet reproducing the issue. I am assuming that you are using latest Aspose.Email for .NET 20.5 on your end.

Here it is. but this OST File is too private. (It is a file of my customer)
If you need this file, Can you tell me how to give it privately.

And It is my code.

   private void extractXSTFiles(string path) {
        string folder = "\\\\?\\" + Path.GetDirectoryName(path) + "\\Extracted→" + Path.GetFileName(path);

        if (Directory.Exists(folder)) //추출할 폴더 생성
        {
            return; //이미 추출했다면 리턴
        }
        else
        {
            Directory.CreateDirectory(folder);
        }

        try
        {
            //ost 는 root폴더 앞에 \\가 붙음
            if (Path.GetExtension(folder).ToUpper() == ".OST")
            {

            }
            else if (Path.GetExtension(folder).ToUpper() == ".PST")
            {
                folder += "\\";
            }

            using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
            {

                Directory.CreateDirectory(folder);

                // load the Outlook PST file
                PersonalStorage pst = PersonalStorage.FromStream(stream);

                // get the folders and messages information
                FolderInfo folderInfo = pst.RootFolder;


                // Call the recursive method to extract msg files from each folder
                xstExtract(folderInfo, pst, folder);
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
        }

    }

    private void xstExtract(FolderInfo folderInfo, PersonalStorage pst, string folder)
    {

        string currentFolder = folder + folderInfo.DisplayName + "\\";


        if (!Directory.Exists(currentFolder))
        {
            Directory.CreateDirectory(currentFolder);
        }

        //메일 시간 + 제목 + 랜덤숫자로 추출
        foreach (MapiMessage message in folderInfo.EnumerateMapiMessages())
        {
            message.Save(currentFolder + GetFileName(message.Subject, message.DeliveryTime) + ".msg", Aspose.Email.SaveOptions.DefaultMsg);
        }
        // Call this method recursively for each subfolder
        if (folderInfo.HasSubFolders == true)
        {
            foreach (FolderInfo subfolderInfo in folderInfo.EnumerateFolders())
            {
                xstExtract(subfolderInfo, pst, currentFolder);
            }
        }
    }

@HM_Company,

I am unable to find any OST file. You can share privately with us by sending Message. All you need to do is click on my name Icon and use Message option to send privately. But do add the forum thread link in your message so that I can understand that for which thread you have shared the data.

image.png (9.7 KB)

@HM_Company,

I have tested the OST file shared by you using your sample code. There are issues in sample code when saving MSG files on disk during extraction. You are using message subject and delivery time as file name during saving MSG on disk. They contain the illegal characters that are not allowed by MS Windows to be used as file names. Therefore, one need to trim such illegal characters from the name string. The other important thing you need to consider is that MS allows the file name and path not exceeding 255 characters too. I suggest you to please try using following modified sample code on your end to serve the purpose. Therefore, any other illegal character you find, you will have to remove that.

    public static void extractXSTFiles(string path = @"C:\Test\300back@gmail.com.ost")
    {
        try
        {

   //         string folder = "\\\\?\\" + Path.GetDirectoryName(path) + "\\Extracted" + Path.GetFileName(path);
            string folder =  Path.GetDirectoryName(path) + "\\Extracted" + Path.GetFileName(path);
            // String path = @"C:\Users\mudas\Downloads\";

            using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
            {

                Directory.CreateDirectory(folder);

                // load the Outlook PST file
                PersonalStorage pst = PersonalStorage.FromStream(stream);

                // get the folders and messages information
                FolderInfo folderInfo = pst.RootFolder;


                // Call the recursive method to extract msg files from each folder
                xstExtract(folderInfo, pst, folder);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            Debug.WriteLine(ex.Message);
        }

    }

    private static void xstExtract(FolderInfo folderInfo, PersonalStorage pst, string folder)
    {

        string currentFolder = folder + folderInfo.DisplayName + "\\";


        if (!Directory.Exists(currentFolder))
        {
            Directory.CreateDirectory(currentFolder);
        }

        //메일 시간 + 제목 + 랜덤숫자로 추출
        foreach (MapiMessage message in folderInfo.EnumerateMapiMessages())
        {
            try
            {
                //message.Save(currentFolder + GetFileName(message.Subject, message.DeliveryTime) + ".msg", Aspose.Email.SaveOptions.DefaultMsg);
                 message.Save(currentFolder + Regex.Replace(message.Subject, "[�*?<>/:@,\\.\";'\\\\🔴]", string.Empty) + ".msg", Aspose.Email.SaveOptions.DefaultMsg);

            }
            catch (Exception ex)
            {
                Console.WriteLine("Error saving MSG..");
                Console.WriteLine(ex.Message);
                Debug.WriteLine(ex.Message);
            }


        }
        // Call this method recursively for each subfolder
        if (folderInfo.HasSubFolders == true)
        {
            foreach (FolderInfo subfolderInfo in folderInfo.EnumerateFolders())
            {
                xstExtract(subfolderInfo, pst, currentFolder);
            }
        }
    }

Actually, I used this code too

private static string GetFileName(string subject, DateTime time)
    {
        Random r = new Random();
        string fileName = "";

        if (subject == null || subject.Length == 0)
        {
             fileName = "NoSubject";
            return fileName + "_" + r.Next(1, 1000); 
        }
        else
        {
            if(time != null)
                fileName = time.ToString("yyyy-MM-dd HHmmss") + "_";

           for (int i = 0; i < subject.length; i++)
            {
                if (subject[i] > 31 && subject[i] < 127)
                {
                    filename += subject[i];
                }
            }

            fileName = fileName.Replace("\\", "_");
            fileName = fileName.Replace("/", "_");
            fileName = fileName.Replace(":", "_");
            fileName = fileName.Replace("*", "_");
            fileName = fileName.Replace("?", "_");
            fileName = fileName.Replace("\"", "_");
            fileName = fileName.Replace("<", "_");
            fileName = fileName.Replace(">", "_");
            fileName = fileName.Replace("|", "_");
            fileName = fileName.Replace("\n", "");
            fileName = fileName.Replace("\r", "");
            fileName = fileName.Replace("\t", "");
            fileName = fileName.Replace("\u000e", "");
            
            return fileName + "_" + r.Next(1, 100000);//; 
        }
    }

So, You don’t have to consider some special character.
And, In my case “\\?\” works in path exceeding 255 characters.

But the main problem, Did you try OST file that i give you really?
I said the EndOfStreamException was the problem.
And your code also occurs theEndOfStreamException in “synchronization problems folder” too.

Can you try one more time?

@HM_Company,

I have tested your sample OST yesterday but owing to exception handling the message was not printed. The problem occurs in the end of your OST file where a folder with empty name is extracted and there following problem occurs. I have created a ticket with ID EMAILNET-39857 in our issue tracking system to further investigate this on our end.
Please also don’t remove access to the source OST file as well till our team access that.

Error extracting folder :
Unable to read beyond the end of the stream.
at System.IO.BinaryReader.FillBuffer(Int32 numBytes)
at System.IO.BinaryReader.ReadUInt32()
at #=zuYopK_XwBuorY7$Wyzrl23Hd5KERpMxdZn$a4DU=.#=zkaZpOHj4S6c8(Byte[] #=zp09JO7s=, Byte #=zfbkKfM8=)
at #=zuYopK_XwBuorY7$Wyzrl23Hd5KERpMxdZn$a4DU=.#=zQztyuV8=()
at #=zuYopK_XwBuorY7$Wyzrl23Hd5KERpMxdZn$a4DU=…ctor(#=zkbuahBunWE8iH6a4B$WAN2X1HSxRPH2YZA== #=z1fRgC2Q=, UInt32 #=ztsNVyqT2fZ6NRGbVRw==)
at #=z8qBukjg3EV$9F8tXQHPsgyCXTpAWH8Dtgw==…ctor(#=zkbuahBunWE8iH6a4B$WAN2X1HSxRPH2YZA== #=z1fRgC2Q=, #=zjrHo0Eej5S68W3_3e22men04r37kEDfIX$v7$1M= #=zPHVUB_K6nX9eo5CIzQ==, #=z2o0CJQWN9SvhxufNeoTGHwksp27keVbzitzuQ3YLj1AM #=z_4VaCaPY6j8c)
at #=z2o0CJQWN9SvhxufNeoTGHwksp27keVbzitzuQ3YLj1AM.#=zXzCLU6Q=(#=z17_NZbFb$uBOOI7IqZX_98DHd9i2txV9XA== #=z$iXxF5onBRFU)
at #=z2o0CJQWN9SvhxufNeoTGHwksp27keVbzitzuQ3YLj1AM.#=zBHPfLy79ldQV(#=z17_NZbFb$uBOOI7IqZX_98DHd9i2txV9XA== #=zzwPFkEU=, Int32 #=zYYhgDMQ=)
at #=z2o0CJQWN9SvhxufNeoTGHziw_LBWCOB7Hwwh7hE=.#=zDGTv4ubwQtXVwDZygQ==(#=z17_NZbFb$uBOOI7IqZX_98DHd9i2txV9XA== #=zMXEAn8qMsH0v)
at Aspose.Email.Storage.Pst.FolderInfo.EnumerateMapiMessages()
at TestEmail.Program.xstExtract(FolderInfo folderInfo, PersonalStorage pst, String folder) in C:\Users\mudas\source\repos\TestEmail\TestEmail\Program.cs:line 1079
at TestEmail.Program.xstExtract(FolderInfo folderInfo, PersonalStorage pst, String folder) in C:\Users\mudas\source\repos\TestEmail\TestEmail\Program.cs:line 1103
at TestEmail.Program.xstExtract(FolderInfo folderInfo, PersonalStorage pst, String folder) in C:\Users\mudas\source\repos\TestEmail\TestEmail\Program.cs:line 1103
at TestEmail.Program.xstExtract(FolderInfo folderInfo, PersonalStorage pst, String folder) in C:\Users\mudas\source\repos\TestEmail\TestEmail\Program.cs:line 1103
at TestEmail.Program.extractXSTFiles(String path) in C:\Users\mudas\source\repos\TestEmail\TestEmail\Program.cs:line 1043

1 Like

Hasn’t it been modified yet?
I found another issue in OST file. Error message said “Pade data is not valid” in Inbox folder
But I could extract messeges in this OST file with other tools (ex. Kernel)

@HM_Company,

Apparently there is a corruption in the folder’s contents table. For the such cases we have an overloaded FolderInfo.GetContents(bool tryToReadCorruptedContents) method which gets the messages bypassing the contents table reading.

Please just pass the parameter tryToReadCorruptedContents = true to get messages from the corrupted folder:

FolderInfo syncIssues = pst.GetFolderById("AAAAAC59obw1CmhIv993He3SbGpigAAA");
MessageInfoCollection messages = syncIssues.GetContents(true);

I’ve been waiting for this answer for a long time.
It’s a little late, but thank you for solving it.

But, the new issue that i write “Page Data is not valid” is not solved.
I think it is a problem caused by another reason.

foreach (FolderInfo subfolderInfo in folderInfo.EnumerateFolders())

or

foreach (FolderInfo subfolderInfo in folderInfo.GetSubFolders())

“Page Data is not valid” show in this code.

Can you figure out this issue?

@HM_Company

We are sorry for your incovneience.

Can you please provide the working example that you are using to reproduce the issue and we will try the to investigate that on our end and help you out.

I can send a file and codes
But I can’t send you a private message, how should I send it?

@HM_Company

You can upload them on any file server and share the download link with us.

I mean this .ost file is so private. So, Can you give me a email address?

@HM_Company

If you intend to share data privately with us, you need to click on my name icon in my post. A popup will appear and you will see an option “Message”. Use that to send data privately with us as that is private message option.

As I said earlier, I can’t send a message.

image.png (13.4 KB)

@HM_Company

It’s strange as I am able to received private messages from other client. Can you please ensure if you are signed in to thread. You must be able to see the Message option for your self too.

image.png (11.8 KB)

I sent a message. Did you see it?
I could only contact you through old messages.

@HM_Company

I am sorry, I am unable to understand your point. Which message you are referring to please.

I sent again.
If you didn’t get it, give me an e-mail me.