无法使用 C# 访问不同的收件人帐户文件夹 Interop.Outlook
Unable to access a different recipients account folders with C# Interop.Outlook
我正在尝试通过 Outlook 中的其他帐户访问我的垃圾邮件文件夹。
在我的 Outlook 帐户中,我设置了 3 个电子邮件帐户,其中 2 个与工作相关,1 个与个人相关。
我使用以下代码成功访问了我主帐户中的电子邮件:
ns = Application.GetNamespace("MAPI");
inbox = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
items = inbox.Items;
这非常有效。
但是,当我尝试访问其他帐户时,出现以下错误:
System.Runtime.InteropServices.COMException: 'The operation failed because of a registry or installation problem. Restart Outlook and try again. If the problem persists, reinstall.'
at Microsoft.Office.Interop.Outlook.NameSpaceClass.GetSharedDefaultFolder(Recipient Recipient, OlDefaultFolders FolderType)
at DemoAntiSpam2.ThisAddIn.ThisAddIn_Startup(Object sender, EventArgs e) in C:\Projects\T\DemoAntiSpam2\DemoAntiSpam2\ThisAddIn.cs:line 29
at Microsoft.Office.Tools.AddInImpl.OnStartup()
at DemoAntiSpam2.ThisAddIn.FinishInitialization() in C:\Projects\T\DemoAntiSpam2\DemoAntiSpam2\ThisAddIn.Designer.cs:line 57
at Microsoft.VisualStudio.Tools.Office.Runtime.DomainCreator.ExecuteCustomization.ExecutePhase(ExecutionPhases executionPhases)
at Microsoft.VisualStudio.Tools.Office.Runtime.DomainCreator.ExecuteCustomization.Microsoft.VisualStudio.Tools.Office.Runtime.Interop.IExecuteCustomization2.ExecuteEntryPoints()
Outlook.NameSpace ns;
Outlook.MAPIFolder inbox;
Outlook.Items items;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
ns = Application.GetNamespace("MAPI");
var recipient = ns.CreateRecipient(recipientAddress);
recipient.Resolve();
if (recipient.Resolved)
{
// Code breaks here.
inbox = ns.GetSharedDefaultFolder(recipient, Outlook.OlDefaultFolders.olFolderInbox);
items = inbox.Items;
items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(items_ItemAdd);
}
}
我已经设法解决了以下问题:
我没有使用 GetSharedDefaultFolder()
访问文件夹,而是查找了命名空间文件夹的索引:
Outlook.NameSpace ns = Application.GetNamespace("Mapi");
Outlook.MAPIFolder folder = ns.Folders[recipient].Folders["Ongewenste e-mail"];
文件夹名称是您在 outlook 中使用的直接名称。您还可以使用 EntryId
,它是对象的唯一 ID。
我正在尝试通过 Outlook 中的其他帐户访问我的垃圾邮件文件夹。 在我的 Outlook 帐户中,我设置了 3 个电子邮件帐户,其中 2 个与工作相关,1 个与个人相关。
我使用以下代码成功访问了我主帐户中的电子邮件:
ns = Application.GetNamespace("MAPI");
inbox = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
items = inbox.Items;
这非常有效。
但是,当我尝试访问其他帐户时,出现以下错误:
System.Runtime.InteropServices.COMException: 'The operation failed because of a registry or installation problem. Restart Outlook and try again. If the problem persists, reinstall.'
at Microsoft.Office.Interop.Outlook.NameSpaceClass.GetSharedDefaultFolder(Recipient Recipient, OlDefaultFolders FolderType)
at DemoAntiSpam2.ThisAddIn.ThisAddIn_Startup(Object sender, EventArgs e) in C:\Projects\T\DemoAntiSpam2\DemoAntiSpam2\ThisAddIn.cs:line 29
at Microsoft.Office.Tools.AddInImpl.OnStartup()
at DemoAntiSpam2.ThisAddIn.FinishInitialization() in C:\Projects\T\DemoAntiSpam2\DemoAntiSpam2\ThisAddIn.Designer.cs:line 57
at Microsoft.VisualStudio.Tools.Office.Runtime.DomainCreator.ExecuteCustomization.ExecutePhase(ExecutionPhases executionPhases) at Microsoft.VisualStudio.Tools.Office.Runtime.DomainCreator.ExecuteCustomization.Microsoft.VisualStudio.Tools.Office.Runtime.Interop.IExecuteCustomization2.ExecuteEntryPoints()
Outlook.NameSpace ns;
Outlook.MAPIFolder inbox;
Outlook.Items items;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
ns = Application.GetNamespace("MAPI");
var recipient = ns.CreateRecipient(recipientAddress);
recipient.Resolve();
if (recipient.Resolved)
{
// Code breaks here.
inbox = ns.GetSharedDefaultFolder(recipient, Outlook.OlDefaultFolders.olFolderInbox);
items = inbox.Items;
items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(items_ItemAdd);
}
}
我已经设法解决了以下问题:
我没有使用 GetSharedDefaultFolder()
访问文件夹,而是查找了命名空间文件夹的索引:
Outlook.NameSpace ns = Application.GetNamespace("Mapi");
Outlook.MAPIFolder folder = ns.Folders[recipient].Folders["Ongewenste e-mail"];
文件夹名称是您在 outlook 中使用的直接名称。您还可以使用 EntryId
,它是对象的唯一 ID。