Code olm to pst

bool ConvertOlmToPst(string olmFilePath, string fullPstPath)//last
{

  // Create a new PST
  PersonalStorage pst = PersonalStorage.Create(fullPstPath, FileFormatVersion.Unicode);
  var root = pst.RootFolder;

  FolderInfo inboxFolder = pst.CreatePredefinedFolder("Inbox", StandardIpmFolder.Inbox);
  ageDict.Add("Inbox", inboxFolder.ContainerClass);
  FolderInfo sent = pst.CreatePredefinedFolder("Sent Items", StandardIpmFolder.SentItems);
  ageDict.Add("Sent Items", sent.ContainerClass);
  FolderInfo drafts = pst.CreatePredefinedFolder("Drafts", StandardIpmFolder.Drafts);
  ageDict.Add("Drafts", drafts.ContainerClass);
  FolderInfo junk = pst.CreatePredefinedFolder("Junk Email", StandardIpmFolder.JunkEmail);
  ageDict.Add("Junk Email", junk.ContainerClass);
  FolderInfo calendar = root.AddSubFolder("Calendar", "IPF.Appointment");
  ageDict.Add("Calendar", calendar.ContainerClass);
  FolderInfo contacts = root.AddSubFolder("Contacts", "IPF.Contact");
  ageDict.Add("Contacts", contacts.ContainerClass);
  FolderInfo journal = root.AddSubFolder("Journal", "IPF.Journal");
  ageDict.Add("Journal", journal.ContainerClass);
  FolderInfo notes = pst.CreatePredefinedFolder("Notes", StandardIpmFolder.Notes);
  ageDict.Add("Notes", notes.ContainerClass);
  FolderInfo tasks = root.AddSubFolder("Tasks", "IPF.Task");
  ageDict.Add("Tasks", tasks.ContainerClass);
  //Add redifine folder Types//.............................//
  ageDict.Add("Address Book", "IPF.Contact");
  ageDict.Add("Suggested Contacts", "IPF.Contact");

  ageDict.Add("Junk E - mail", "IPF.Note");
  ageDict.Add("Outbox", "IPF.Note");
  ageDict.Add("Deleted Items", "IPF.Note");
  ageDict.Add("All", "IPF.Note");
  ageDict.Add("Follow-Up", "IPF.Note");
  ageDict.Add("Inbox-Categorized1", "IPF.Note");//Suggested Contacts
                                                //--------------------------------------------------------//

  OlmStorage olm = new OlmStorage(olmFilePath);
  nTotalCount = GetOlmTotalItemCount(olm);
  //nTotalCount = GetTotalItems(olm);
  if (nTotalCount == 0)
  {
      //Console.WriteLine("⚠️ No items found in OLM file.");
      return false;
  }

  List<OlmFolder> folders = GetAllOlmFolders(olmFilePath);       
  string Type;
  for (int i = 0; i < folders.Count; i++)
  {
      var folder = folders[i];           
      if (ageDict.TryGetValue(folder.Name, out Type))
      {
          //Console.WriteLine($"Alice's age is {Type}");
      }            
      else
      {
          Type = DetectFolderType(olm,folder);
          if (string.IsNullOrEmpty(Type))
          {
              Type = "IPM.Note";
          }
      }          
      if (folder.Name != "Accounts")
      {
          FolderInfo folderfull = EnsureFolderPath(pst, folder.Path, Type);
          try
          {
              try
              {
                  // ✅ Enumerate messages safely
                  var messages = olm.EnumerateMessages(folder);
                  if (messages == null)
                      return false;

                  foreach (MapiMessage mapi in messages) // ✅ Correct type for v25.11
                  {
                      try
                      {
                          if (mapi == null)
                              continue;

                          // ✅ Handle calendar items safely
                          if (mapi.MessageClass.StartsWith("IPM.Appointment", StringComparison.OrdinalIgnoreCase))
                          {
                              var calItem = (MapiCalendar)mapi.ToMapiMessageItem();  // renamed variable

                              // Sanitize invalid dates
                              if (calItem.StartDate < new DateTime(1900, 1, 1))
                                  calItem.StartDate = DateTime.Now;

                              if (calItem.EndDate < new DateTime(1900, 1, 1) || calItem.EndDate < calItem.StartDate)
                                  calItem.EndDate = calItem.StartDate.AddHours(1);

                              folderfull.AddMapiMessageItem(calItem);
                          }
                          else if (mapi.MessageClass.StartsWith("IPM.Contact", StringComparison.OrdinalIgnoreCase))
                          {
                              var contactItem = (MapiContact)mapi.ToMapiMessageItem();                                   
                              try
                              {                                        
                                  folderfull.AddMapiMessageItem(contactItem);                                      
                              }
                              catch (Exception ex1)
                              {
                                 // Console.WriteLine($"⚠️ Skipped message in folder {folder.Name}: {ex1.Message}");
                              }
                          }

                          else if (mapi.MessageClass.StartsWith("IPM.Task", StringComparison.OrdinalIgnoreCase))
                          {
                              var task = (MapiTask)mapi.ToMapiMessageItem();
                              folderfull.AddMapiMessageItem(task);
                          }
                          else if (mapi.MessageClass.StartsWith("IPM.StickyNote", StringComparison.OrdinalIgnoreCase))
                          {
                              var note = (MapiNote)mapi.ToMapiMessageItem();
                              folderfull.AddMapiMessageItem(note);
                          }
                          else if (mapi.MessageClass.StartsWith("IPM.Activity", StringComparison.OrdinalIgnoreCase))
                          {
                              var journalItem = (MapiJournal)mapi.ToMapiMessageItem();
                              folderfull.AddMapiMessageItem(journalItem);
                          }
                          else
                          {
                              folderfull.AddMessage(mapi);
                                                              
                          }

                          totalMessageCount++;
                          if (nTotalCount > 0)
                              progress.Progress = (int)((totalMessageCount * 100) / nTotalCount);

                          WriteProgress();
                      }
                      catch (Exception ex)
                      {
                          //Console.WriteLine($"⚠️ Skipped message in folder {folder.Name}: {ex.Message}");
                      }
                  }                        
              }
              catch (Exception ex)
              {
                 // Console.WriteLine("ConvertOstToPst Exception: " + ex.Message);
                  UpdateProgress("Error");
              }
             
          }

          catch (Exception ey)
          {
             //Console.WriteLine("ConvertOstToPst Exception: " + ey.Message);
              
          }
          //Console.WriteLine($"Folder {i}: {folder.Name}");
          //Console.WriteLine($"Folder {i}: {folder.Path}");
      }
  }
  // ✅ Calculate final PST size
  if (File.Exists(fullPstPath))
  {
      FileInfo pstInfo = new FileInfo(fullPstPath);
      double sizeInMB = pstInfo.Length / (1024.0 * 1024.0);
      progress.Convertedpstsize = $"{sizeInMB:F2} MB";
     
  }
  else
  {
      progress.Convertedpstsize = "0.00 MB";
  }
  pst.Dispose();
  //Console.WriteLine($" Conversion complete: {totalMessageCount} items processed.");
  
  //Console.WriteLine($"Total folders found: {folders.Count}");      
  return true;

}

not woking this approach on contact win32 system file time

@Anjali12

You are trying to convert an OLK (OLM) file to PST format using Aspose.Email for .NET, specifically version 25.11, but you’re encountering issues with contact items and Win32 system file times .

The code you’ve shared attempts to enumerate messages from each folder in the OLM file and adds them to corresponding folders in the PST, handling different MAPI message types such as calendar items, contacts, tasks, notes, and journal entries . However, there isn’t explicit information in the provided context confirming a specific bug or limitation related to contact item conversion or Win32 system file times in version 25.11 .

To help diagnose the problem further:

  1. Please confirm if the issue manifests as exceptions being thrown during the contact item processing, or if the contacts simply don’t appear in the resulting PST.
  2. If possible, provide a minimal sample OLM file that reproduces the issue.
  3. Check the inner exception details if an exception occurs during the AddMapiMessageItem call for contact items.

If you can reproduce the issue consistently, it would be beneficial to log it internally for investigation by the Aspose team.

i used version 25.12

@Anjali12

You are trying to convert an OLK (OLM) file to PST format using Aspose.Email for .NET version 25.12, and you’re encountering issues with contact items related to “Win32 system file times” . Specifically, the error “Not a valid Win32 FileTime” is thrown when processing contact items, likely due to invalid DateTime values in contact properties .

This appears to be a known limitation or bug in Aspose.Email when handling certain contact items from OLK files . While your code attempts to process various MAPI message types including contacts, it fails during the conversion of contact items due to these invalid time values .

To resolve this issue, consider the following steps:

  1. Catch and handle the exception during contact item processing to skip problematic entries.
  2. Log the inner exception details for further investigation by Aspose support if the issue persists.
  3. If possible, test with a minimal sample OLM file to isolate the problem .

If you can consistently reproduce the issue, it would be beneficial to log it internally for investigation by the Aspose team .

Hello,

The issue you’re encountering with contact items and Win32 time values is unrelated to your folder loop logic — it’s caused by invalid DateTime values inside some contact properties, which the PST format does not accept.
Aspose.Email throws “Not a valid Win32 FileTime” when it encounters such values.

Your current approach processes contacts like this:

if (mapi.MessageClass.StartsWith("IPM.Contact", StringComparison.OrdinalIgnoreCase))
{
    var contactItem = (MapiContact)mapi.ToMapiMessageItem();
    folderfull.AddMapiMessageItem(contactItem);
}

This will fail if the contact item contains any date/time fields outside the valid range for Win32 FILETIME.

What to do next:

Try to wrap contact item handling in a try/catch and sanitize date fields before adding to PST. For example:

try
{
    var contactItem = (MapiContact)mapi.ToMapiMessageItem();

// Example sanitization
if (contactItem.Birthday < DateTime.MinValue || contactItem.Birthday > DateTime.MaxValue)
    contactItem.Birthday = DateTime.Now;

folderfull.AddMapiMessageItem(contactItem);
}
catch (Exception ex)
{
    // Log or skip problematic contact
}

This prevents the invalid Win32 file time error by normalizing or skipping invalid dates.

  • Are contact items absent in the final PST, or do you get a runtime error? The exact exception text and stack trace help pinpoint the problem.

  • Test with a minimal OLM sample that only contains a few contact items. If that reproduces the issue consistently, you can isolate whether the problem is specific to certain contacts. If possible, share that minimal sample with support for deeper investigation.

Please note:

  • The code you posted is not inherently wrong, but Aspose.Email will fail when contact items contain invalid date/time fields that cannot be represented as Win32 FILETIME.

  • You must handle those cases explicitly (skip or normalize) to avoid conversion errors.

  • Providing exact error details makes it much easier for others to help debug.

  • Please confirm what you observe (exception vs missing items) and post the error details if you still need assistance.

Thank you.

Sorry for jumping in a thread not owned by me, just wanted to know if the source olm was generated by Outlook for Mac and saved from the Mac or it was made by 3rd party apps? :slight_smile: