提问人:MOMO 提问时间:4/21/2023 最后编辑:Eugene AstafievMOMO 更新时间:4/26/2023 访问量:51
使用 EWS 从 Outlook 提取用户时获取空集合
Getting empty collection while fetching the users from outlook with EWS
问:
从 Outlook 获取用户时获取空集合。也许有人熟悉这个问题。
这就是我设置交换服务的方式。这是非常标准的,并遵循 Microsoft 中的文档:
exchangeService.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
exchangeService.Credentials = new OAuthCredentials(SMTPClientFactory.Secret());
exchangeService.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "example@com");
exchangeService.HttpHeaders.Add("X-AnchorMailbox", "example@com");
当然,我使用了共享邮箱的地址。
我必须使用 EWS 从 Outlook 中获取用户,但一直走到死胡同。我尝试使用功能
var allUsers = exchangeService.ResolveName("SMTP:", ResolveNameSearchLocation.ContactsThenDirectory, true);
但是这个函数有局限性,它不能获取超过100个用户。
我也尝试使用这段代码
SearchFilter searchFilter = new SearchFilter.ContainsSubstring(ContactSchema.EmailAddress1, "@");
// Use a FindPeopleResults object to retrieve the contacts.
FindPeopleResults results = exchangeService.FindPeople(WellKnownFolderName.Contacts, searchFilter, new ItemView(int.MaxValue)).Result;
但这并不能获取任何东西。还尝试使用功能,它也没有给我任何东西。FindItem
目标是使用 fetch all users。
答:
foreach (Item item in results.Items)
{
EmailAddress user = item as EmailAddress;
if (user != null)
{
NameResolutionCollection resolutions = service.ResolveName(user.Address, ResolveNameSearchLocation.DirectoryOnly, true);
foreach (NameResolution resolution in resolutions)
{
Contact contact = resolution.Contact;
if (contact != null)
{
Console.WriteLine(contact.DisplayName);
}
}
}
}
评论
这种事情
SearchFilter searchFilter = new SearchFilter.IsEqualTo(ContactSchema.EmailAddress1, "*");
FindPeopleResults results = exchangeService.FindPeople(WellKnownFolderName.Contacts, searchFilter, new ItemView(int.MaxValue)).Result;
在 EWS 中不起作用,因为 FindPeople 操作不支持 searchFilters https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/findpeople-operation。他们在托管 API 中实现 FindPeople 的方式不正确,唯一支持的搜索限制是 AQS 查询字符串,因此
FindPeopleResults results = exchangeService.FindPeople(WellKnownFolderName.Contacts, "SMTP:", new ItemView(int.MaxValue)).Result
应该工作,但 int。MaxValue 不会执行任何操作,EWS 最多一次将结果分页回 100 个项目。
看起来您正在使用 Office365,因此 EWS 不是用于此目的的最佳终结点。例如,如果您只使用 Graph 和高级查询,您可以更快地获取所有目录对象 https://learn.microsoft.com/en-us/graph/aad-advanced-queries?tabs=http
https://graph.microsoft.com/v1.0/users?$count=true&$filter=proxyAddresses/any (p:startswith(p, 'SMTP:'))&$select=id,displayName,proxyaddresses,mailNickname,userType&$top=999
如果尝试生成 Outlook 类型功能,则应使用搜索终结点,因为这是启用所有新特性和功能的位置 https://learn.microsoft.com/en-us/graph/search-concept-person
下一个:找不到空引用异常 [重复]
评论