Add Msg File to PST

Add msg file to or email to pst in outlook using Microsoft.Office.Interop.Outlook.

To Add physical MSG files to PST and generate PST we need to follow below steps:
  1. Add Com library Microsoft.Office.interop.outlook
  2. Create object of COM Object MailItem,Namespace,Application
  3. ns(object of namespace) = app(object of Applciation).GetNamespace("MAPI")

    ns.AddStoreEx(newPstFileLocation, OlStoreType.olStoreUnicode)
     
  4. For Each store As Store In ns.Session.Stores
                    If store.FilePath = newPstFileLocation Then
                        newPST = store
                    End If
                Next
     
  5. Object of MAPI Folder Ex: oFolder
  6. oFolder = newPST.GetRootFolder
  7. .mail = CType(ns.Session.OpenSharedItem(filetobeprocessed), MailItem)

    mail.Move(folderInbox)

    folderInbox is object of Simple inbox folder and we are adding moving files to folderInbox.