2
votes

I trying to export a mailbox (of any user) from a Exchange 2010 Server to a PST file. What is the best way to archive this?

First I was looking at the COM-Interop model. But this seems just to be able to export the Mailbox of the user that is currently logged in into Outlook. Or am I missing something there? I tried this:

Dim app As New Outlook.Application()
Dim ns As Outlook.NameSpace = app.GetNamespace("MAPI")
ns.AddStore("C:\backup.pst")
Dim backupFolder As Outlook.MAPIFolder = ns.Session.Folders.GetLast()
Dim selectedMailBox As Outlook.MAPIFolder = ns.Folders("[email protected]") 'NOT Working
selectedMailBox.CopyTo(backupFolder)
ns.RemoveStore(backupFolder)

Later I found this PowerShell function: New-MailboxExportRequest Sadly I can't find a way to call this from .NET. Is there any possibility to call it from .NET?

Or do I need to use any other library to export files from Exchange? (Maybe use POP3 to get all Mails and convert them to PST?)

3

3 Answers

1
votes

You can use Redemption for that - run your code as a domain user who can access the mailboxes in question and call RDOSession.LogonExchangeMailbox. You can then open other users' mailboxes using RDOSession.Stores.GetSharedMailbox. Once you have the RDOStore object, you will be able to access all of the folders and messages starting with RDOStore.IPMRootFolder.

To copy to a PST store, you can add a new or existing PST file using RDOSession.Stores.AddPstStore.

To copy all the folders, using something like the following

foreach (RDOFolder sourceFolder in ExchangeStore.IPMRootFolder.Folders)
{
  sourceFolder.CopyTo(PstStore.IPMRootFolder);
}
0
votes

You can execute Powershell commands in C# using System.Management.Automation and System.Management.Automation.Runspaces.

a very limited example:

int GetMiliseconds()
{
    using (var ps = System.Management.Automation.PowerShell.Create())
    {
        var cmd = new System.Management.Automation.Runspaces.Command("Get-Date");
        ps.Commands.AddCommand(cmd);
        var result = ps.Invoke();
        return result != null && result.Any() ? result.First().Members["Millisecond"].Value as Int32? ?? 0 : 0;
    }
}  

The trickiest part is that result always comes back as ICollection<PSObject>, and you have to keep your eyes open for exceptions thrown and null values all the time.

0
votes

which exchange server? In exchange server 2013, you have to first give rights for this role as even the administrator doesn't have them. Open Windows Power-Shell with administrative privileges the do this command:

New-ManagementRoleAssignment –Role “Mailbox Import Export” –User "username"

(REPLACE "username" WITH THE USERNAME).

Create a shared network folder and remember the path as in \"SERVER-NAME"\"SHAREDFOLDERNAME".

Replace "SERVER-NAME" with your server name and "SHAREDFOLDERNAME" with your specific shared folder name. On Exchange Server Management shell (Command Line Interface),use the command Get-Mailbox to get all the mailboxes you have in your exchange server and from there, you can choose the mailbox whose .pst file you want to export. Next, on the CLI, type:

New-MailboxExportRequest –Mailbox "USERNAME" –FilePath \"SERVER-NAME"\"SHAREDFOLDERNAME"\"PSTFILENAME".pst

Again remember to replace "SERVER-NAME" and "USERNAME" with your servername and the specific mailbox's username to be exported otherwise, an error will occur. Replace "PSTFILENAME" with the name as you would like to save your file. After then, you can check the shared folder and you'll notice the size of the file increasing or use the command: Get-MailboxExportRequest -Mailbox "SPECIFICUSERNAME" | format-table -wrap -autosize

to view if the process is done or not. if it's done it'll indicate as "Completed" in the status column. If not, it'll indicate "queued".