gpt4 book ai didi

c# - 无法为此 session 加载 Outlook 数据文件 (.pst)

转载 作者:太空狗 更新时间:2023-10-29 21:57:54 30 4
gpt4 key购买 nike

我正在使用 Microsoft.Office.Interop.Outlook 版本 12.0.0.0 来读取我的 outlook pst 文件但是当编译器到达此代码时 outlookNs.AddStore(pstFilePath); 它给出了异常 “为此 session 加载 Outlook 数据文件 (.pst) 失败。”我也尝试过 outlookNs.AddStoreEx(pstFilePath); 但错误是一样的......任何建议??

using System;
using System.Collections.Generic;
using Microsoft.Office.Interop.Outlook;

namespace PSTReader {
class Program {
static void Main () {
try {
IEnumerable<MailItem> mailItems = readPst(@"C:\temp\PST\Test.pst", "Test PST");
foreach (MailItem mailItem in mailItems) {
Console.WriteLine(mailItem.SenderName + " - " + mailItem.Subject);
}
} catch (System.Exception ex) {
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}

private static IEnumerable<MailItem> readPst(string pstFilePath, string pstName) {
List<MailItem> mailItems = new List<MailItem>();
Application app = new Application();
NameSpace outlookNs = app.GetNamespace("MAPI");
// Add PST file (Outlook Data File) to Default Profile
outlookNs.AddStore(pstFilePath);
MAPIFolder rootFolder = outlookNs.Stores[pstName].GetRootFolder();
// Traverse through all folders in the PST file
// TODO: This is not recursive, refactor
Folders subFolders = rootFolder.Folders;
foreach (Folder folder in subFolders) {
Items items = folder.Items;
foreach (object item in items) {
if (item is MailItem) {
MailItem mailItem = item as MailItem;
mailItems.Add(mailItem);
}
}
}
// Remove PST file from Default Profile
outlookNs.RemoveStore(rootFolder);
return mailItems;
}
}
}

最佳答案

我遇到了同样的问题,下面是我所做的。

当你说 outlookNs.AddStore 时,你必须给出路径和文件名。然后在 outlookNs.Stores 中,变量 pstName 的扩展名应为 .pst,您必须将其删除。

下面是我如何让它工作的示例。

public class Mail
{
public MailItem mailItem { get; set; }
public String path { get; set; }
}


public static class Mails
{
public static List<Mail> readPst(string pstFilePath, string pstName)
{
List<Mail> mail = new List<Mail>();
Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
NameSpace outlookNs = app.GetNamespace("MAPI");

// Add PST file (Outlook Data File) to Default Profile
outlookNs.AddStore(pstFilePath + pstName);

string storeInfo = null;

foreach (Store store in outlookNs.Stores)
{
storeInfo = store.DisplayName;
storeInfo = store.FilePath;
storeInfo = store.StoreID;
}

MAPIFolder rootFolder = outlookNs.Stores[pstName.Substring(0,pstName.Length-4)].GetRootFolder();

// Traverse through all folders in the PST file
Folders subFolders = rootFolder.Folders;

foreach (Folder folder in subFolders)
{
ExtractItems(mail, folder);
}
// Remove PST file from Default Profile
outlookNs.RemoveStore(rootFolder);
return mail;
}

private static void ExtractItems(List<Mail> mailItems, Folder folder)
{
Items items = folder.Items;

int itemcount = items.Count;

foreach (object item in items)
{
if (item is MailItem)
{
MailItem mailItem = item as MailItem;
Mail mail = new Mail();
mail.mailItem = mailItem;
mail.path = folder.FolderPath + folder.Name;
mailItems.Add(mail);
}
}

foreach (Folder subfolder in folder.Folders)
{
ExtractItems(mailItems, subfolder);
}
}
}

关于c# - 无法为此 session 加载 Outlook 数据文件 (.pst),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22190976/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com