Free Support Forum - aspose.com

How to save Items from Deleted Items from Office 365 Mailbox to a PST file

How to save Items from Deleted Items ( Containing contact, tasks, notes and message items) from Office 365 Mailbox to a PST file. please share c# code.

thanks

@ravikumarchopra,

Please check the following sample code that iterate through the PST folders. You can check the Deleted Items folders and extract the messages from them.

public static void TestOlm()
{
    String dataDir = "C:\\Email\\Outlook2\\";
    OlmStorage storage = new OlmStorage(dataDir + "Outlook2.olm");
    // The path to the File directory.

    foreach (OlmFolder folder in storage.FolderHierarchy)
    {
        ReadFolders(folder, storage);
     }
}	
public static void ReadFolders(OlmFolder folder, OlmStorage storage)
{
    if (folder.SubFolders.Count > 0)
    {
        foreach (OlmFolder sub_folder in folder.SubFolders)
        {
            ReadFolders(sub_folder, storage);
        }
    }
    else
    {
        Console.WriteLine("Subfolder: " + folder.Name);

        if(folder.Name.Contains("Deleted Items"))
        {
            if (folder.HasMessages)
            {
                // extract messages from folder
                foreach (MapiMessage msg in storage.EnumerateMessages(folder))
                {
                    Console.WriteLine("Subject: " + msg.Subject);
                }
            }
        }
    }
}