I'm trying to copy all emails from one folder of my main PST to a new PST I create, then SFTP that PST over. I have the PST being created correctly, the files getting copied over, and then it getting removed from them navigation bar, but it is still being used by Outlook for some reason, which won't let me open it for reading. Here is what I have:
public static Outlook.Application oApp = new Outlook.Application();
public static Outlook.NameSpace oNS = oApp.GetNamespace("MAPI");
public static Outlook.MAPIFolder RootFolder;
public static void CopyMessages()
{
string folderName = "emails";
string strFileName = @"C:\test.pst";
string strDisplayName = "PST";
Outlook.MAPIFolder inBox = oApp.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
ThisAddIn.oNS.AddStore(strFileName);
RootFolder = ThisAddIn.oNS.Folders.GetLast();
RootFolder.Name = strDisplayName;
//ThisAddIn.oNS.RemoveStore(RootFolder);
//ThisAddIn.oNS.AddStore(strFileName);
try
{
inBox.Folders[folderName].CopyTo(RootFolder);
//System.Threading.Thread.Sleep(2000);
ThisAddIn.oNS.RemoveStore(RootFolder);
//ThisAddIn.oNS.AddStore(strFileName);
}
catch
{
MessageBox.Show("There is no folder named " + folderName +
".", "Find Folder Name");
}
}
Any help would be appreciated.
PST provider in Outlook keeps the PST file open for 30 minutes after closing it for the performance reasons.
I don't think there is much you can do using the Outlook Object Model alone. You can create a separate app that uses Extended MAPI (C++ or Delphi) or Redemption (any language) that creates temporary profile and adds the specified PST file to that profile. Once the secondary process exits, the file is released, and your addin will be able to delete it.