Convert both Emlx and Eml to PST

Mail.zip (2.2 MB)
Dear Support,

it is helpful for me the given sample code add only single INBOX folder without the directory structure the INBOX is store from Mail/IMap-chauhan.priyankur1989@imap.gmail.com/INBOX this path and All Mail is store from Mail/IMap-chauhan.priyankur1989@imap.gmail.com/[Gmail]/All Mail.

i select Root Mail folder which is uploaded it has multiple directory such as INBOX, All Mail, Drafts, Important with own emlx file. i want to know how to add all directory with own all E-mail with tree form (same as uploaded directory structure) to converted pst file.

@priyankur.chauhan,

Your requirement is purely related to making logic for reading folders/emails information from disc and create the same in PST. We have written a simple code that does the same for you. Please try it at your end and feel free to make changes to the code if further modification is required.

Sample Code

static void CreatePstWithFolderStructure()
{
    File.Delete("StructuredPst.pst");

    PersonalStorage pst = PersonalStorage.Create("StructuredPst.pst", FileFormatVersion.Unicode);

    AddFilesFromDirectoryToPstFolder(pst.RootFolder, "Mail\\");
}

private static void AddFilesFromDirectoryToPstFolder(FolderInfo folder,  string sDir)
{
    try
    {
        foreach (string f in Directory.GetFiles(sDir, "*.emlx"))
        {
            //convert each Emlx file to MapiMessage and add to folder
            MailMessage emlx = MailMessage.Load(f);

            folder.AddMessage(MapiMessage.FromMailMessage(emlx));
        }
        foreach (string d in Directory.GetDirectories(sDir))
        {
            FolderInfo fi = folder.AddSubFolder(new DirectoryInfo(d).Name);

            AddFilesFromDirectoryToPstFolder(fi, d);
        }
    }
    catch (System.Exception excpt)
    {
        Console.WriteLine(excpt.Message);
    }
}

Output PST File: StructuredPst.zip (2.2 MB)

Dear Support,

Can i remove the Message folder in created structuredPst.zip and store E-mail in main directory such as INBOX, Drafts. Important, All Mail. Because the Mail client show only main directory INBOX, Drafts etc but in database background has multiple unused directory which name Message, 1, 2, 3 etc.so i want to add all main directory such as INBOX, Drafts, Important, All Mail with their E-mail etc and remove the unused directory in created pst file.

The attched screenShort show the my requirement.Screen Shot 2018-05-03 at 9.55.13 AM.png (100.2 KB)

@priyankur.chauhan,

Yes you can modify the C# code as per your requirements and add the messages to the PST file. Please feel free to proceed with it. If you have any further query related to API, don’t hesitate to reach us here on forum for further assistance.