使用的.Net如何创建一个Outlook PST文件? [英] How can I create an Outlook PST file using .Net?

查看:335
本文介绍了使用的.Net如何创建一个Outlook PST文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个应用程序,将操作Outlook数据。我想使该数据第一次备份,我希望我可以通过接触/日历项目只是圈等,并把它们写出来到PST文件。

I'm writing an app that will manipulate Outlook data. I want to make a backup of that data first and am hoping I could just loop through the contact/calendar items, etc and write them out to a PST file.

我怎么能写1使用。NET的内容或几个Outlook文件夹到PST? [VB或者C#无论]

How can I write the contents of 1 or several Outlook folders to a PST using .Net? [vb or c# no matter]

推荐答案

我能够从各种互联网和MSDN文档各地的样本拼凑这个code一起。这将允许您选择一个Outlook高水平的文件夹,然后将备份所有文件夹下面。在我来说,我从未想过邮件文件夹,所以我将其排除。

I was able to piece this code together from a variety of samples around the internet and MSDN docs. This will allow you to choose an outlook high level folder and will backup all folders underneath. In my case I didn't actually want mail folders so I exclude them.

        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

这篇关于使用的.Net如何创建一个Outlook PST文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆