如何使用.Net创建Outlook PST文件?

时间:2009-11-12 19:33:15

标签: .net outlook office-interop

我正在编写一个可以操作Outlook数据的应用。我想首先备份这些数据,并希望我可以遍历联系人/日历项目等,并将它们写入PST文件。

如何使用.Net将1个或多个Outlook文件夹的内容写入PST? [vb或c#无论如何]

1 个答案:

答案 0 :(得分:10)

我能够通过互联网和MSDN文档中的各种示例将此代码拼凑在一起。这将允许您选择Outlook高级文件夹,并将备份下面的所有文件夹。在我的情况下,我实际上并不想要邮件文件夹,所以我排除它们。

        Const BACKUP_PST_PATH As String = "C:\backup.pst"    

        Dim oFolder As Outlook.MAPIFolder = Nothing
        Dim oMailbox As Outlook.MAPIFolder = Nothing

        Dim app As New Outlook.Application()
        Dim ns As Outlook.NameSpace = app.GetNamespace("MAPI")
        Try
            //if the file doesn not exist, outlook will create it
            ns.AddStore(BACKUP_PST_PATH)
            oFolder = ns.Session.Folders.GetLast()
            oMailbox = ns.PickFolder()

         For Each f As Outlook.Folder In oMailbox.Folders
            If f.DefaultItemType <> Microsoft.Office.Interop.Outlook.OlItemType.olMailItem And f.FolderPath <> oFolder.FolderPath Then
                f.CopyTo(oFolder )
            End If
        Next

        ns.RemoveStore(oFolder)

        Catch ex As Exception
            ns.RemoveStore(oFolder)
            IO.File.Delete(BACKUP_PST_PATH)
            Throw ex
        End Try