如何使用 .Net 创建 Outlook PST 文件? [英] How can I create an Outlook PST file using .Net?
问题描述
我正在编写一个可以操作 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.
如何使用 .Net 将 1 个或多个 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]
推荐答案
我能够从 Internet 和 MSDN 文档中的各种示例中拼凑出这段代码.这将允许您选择 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:ackup.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屋!