8

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

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

4

2 回答 2

12

我能够从 Internet 和 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
于 2009-11-25T16:13:06.247 回答
0

C#版本:

public Store CreateStore(string path)
{
    Application outlookApplication = new ();

    Store newPst = null;

    NameSpace outlookNamespace = outlookApplication.GetNamespace("mapi");

    outlookNamespace.Session.AddStore(path);

    foreach (Store store in outlookNamespace.Session.Stores)
    {
        if (store.FilePath == path)
        {
            newPst = store;
            break;
        }
    }

    return newPst;
}
于 2021-11-30T06:28:33.433 回答