如何获取Outlook Contacts指定名称子文件夹中的联系人内容
C# 读取Outlook 365指定联系人子文件夹内容方案
完整可运行代码
直接替换你现有项目中的逻辑即可:
using System; using Outlook = Microsoft.Office.Interop.Outlook; namespace OutlookTest { public class OutlookDataRetriever { public static int Main(string[] args) { try { Outlook.Application oApp = new Outlook.Application(); // 获取默认联系人根目录 Outlook.MAPIFolder rootContactsFolder = oApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts); // 按名称获取指定子文件夹,此处可替换为你需要的目标文件夹名 string targetFolderName = "ThisIsATestGroup"; Outlook.MAPIFolder targetFolder = rootContactsFolder.Folders[targetFolderName]; Console.WriteLine($"成功读取【{targetFolderName}】文件夹下的联系人:"); // 遍历目标文件夹下的所有条目 foreach (var item in targetFolder.Items) { // 筛选普通联系人输出 if (item is Outlook.ContactItem contact) { Console.WriteLine($"- {contact.FullName}"); } // 若需要同时处理联系人组(通讯组列表)可开启下方注释 // else if (item is Outlook.DistListItem distList) // { // Console.WriteLine($"联系人组:{distList.DLName},共{distList.MemberCount}个成员"); // } } } catch (System.Runtime.InteropServices.COMException ex) when (ex.HResult == -2147221233) { Console.WriteLine("错误:未找到指定名称的联系人子文件夹,请检查文件夹名称拼写是否正确"); } catch (Exception e) { Console.WriteLine($"异常捕获:{e}"); } return 0; } } }
补充说明
- 如果你之前打印的子文件夹列表中没有出现
ThisIsATestGroup,说明该文件夹不在当前默认账户的联系人根目录下,可以通过oApp.Session.Stores遍历所有邮箱存储,再逐个读取存储下的联系人目录查找目标文件夹。 - 你之前遍历根联系人目录时遇到的非ContactItem条目,是通讯组列表(DistListItem类型),联系人文件夹只会出现在
Folders集合中,不会出现在Items集合内。
内容的提问来源于stack exchange,提问作者Kristy JN
相关产品推荐
相关产品推荐

