我刚刚发现了Exchanged Web Services Managed API,我一直在玩它以尝试获得理解,因为我相信它可以为我一直在努力解决的问题提供解决方案。 / p>
背景
我已经阅读了很多文档并且对事物有了一般的认识。特别是,我设置了与此LINK非常相似的内容。
但是,我想显示一个文件夹,然后在文件夹后显示所有相关的EmailMessage项目,依此类推,直到所有文件夹/子文件夹。我到目前为止编写的代码将列出一个文件夹,但它会列出我返回的所有电子邮件,而不仅仅是那些与该文件夹相关的电子邮件。我正在搜索未读的项目,只是为了暂时保持列表的小。
代码
private static void GetAllItems(ExchangeService service)
{
List<Folder> lstFolderIds = new List<Folder>();
// Fill list with all public folders and sub-folders
GetAllFolders(service, lstFolderIds);
List<EmailMessage> emails = new List<EmailMessage>();
foreach(Folder folder in lstFolderIds)
{
SearchFilter sf = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
ItemView view = new ItemView(int.MaxValue);
view.PropertySet = PropertySet.IdOnly;
FindItemsResults<Item> searchResults = service.FindItems(folder.Id, sf, view);
if (searchResults.Items.Count > 0)
{
foreach (var item in searchResults.Items)
{
try
{
if (item is EmailMessage)
{
emails.Add((EmailMessage)item);
}
}
catch (Exception ex)
{
Console.WriteLine("Exception: {0}", ex.Message);
Console.WriteLine("Stack Trace: {0}", ex.StackTrace);
Console.WriteLine("Source: {0}", ex.Source);
Console.WriteLine("HResult: {0}", ex.HResult);
}
}
}
}
PropertySet properties = (BasePropertySet.FirstClassProperties);
service.LoadPropertiesForItems(emails, properties);
foreach (Folder folder in lstFolderIds)
{
object folderName;
object displayName;
object childFolderCount;
object unreadCount;
object totalCount;
folderName = folder.TryGetProperty(FolderSchema.ParentFolderId, out folderName) ? folder.ParentFolderId.FolderName.Value.ToString() : "N/A";
displayName = folder.TryGetProperty(FolderSchema.DisplayName, out displayName) ? folder.DisplayName.ToString() : "N/A";
childFolderCount = folder.TryGetProperty(FolderSchema.ChildFolderCount, out childFolderCount) ? folder.ChildFolderCount.ToString() : "N/A";
unreadCount = folder.TryGetProperty(FolderSchema.UnreadCount, out unreadCount) ? folder.UnreadCount.ToString() : "N/A";
totalCount = folder.TryGetProperty(FolderSchema.TotalCount, out totalCount) ? folder.TotalCount.ToString() : "N/A";
Console.WriteLine("Parent Folder: {0}", folderName);
Console.WriteLine("Folder Display Name: {0}", displayName);
Console.WriteLine("Child Folder Count: {0}", childFolderCount);
Console.WriteLine("Folder Unread Count: {0}", unreadCount);
Console.WriteLine("Folder Total Count: {0}", totalCount);
Console.WriteLine("---------------------------------------------------------");
foreach (EmailMessage email in emails)
{
object parentFolderName;
object subject;
object retentionDate;
parentFolderName = email.TryGetProperty(EmailMessageSchema.ParentFolderId, out parentFolderName) ? email.ParentFolderId.FolderName.Value.ToString() : "N/A";
subject = email.TryGetProperty(EmailMessageSchema.Subject, out subject) ? email.Subject.ToString() : "N/A";
retentionDate = email.TryGetProperty(EmailMessageSchema.RetentionDate, out retentionDate) ? retentionDate.ToString() : "N/A";
Console.WriteLine("\tParent Folder Name: {0}", parentFolderName);
Console.WriteLine("\tSubject: {0}", subject);
Console.WriteLine("\tRetention Date: {0}", retentionDate);
//}
}
Console.WriteLine("");
Console.WriteLine("Press or select Enter...");
Console.Read();
}
}
部分问题在于我无法弄清楚如何获取文件夹的文件夹名称或EmailMessage的父文件夹名称。我可以获取文件夹的文件夹ID和EmailMessage的父文件夹ID,但它们对于返回的所有项目都是相同的,所以它一定不对。
我在这篇LINK中读到,我可能需要执行额外的绑定来将文件夹与EmailMessage项目联系起来,但我不确定如何做到这一点。
预期输出
Parent Folder:
Folder Display Name: Inbox
Child Folder Count: 1
Folder Unread Count: 2
Folder Total Count: 51
---------------------------------------------------------
Parent Folder Name: Inbox
Subject: New Coversheets for TPS Reports
Retention Date: 10/31/2017 11:59:59 PM
Parent Folder Name: Inbox
Subject: Have you seen my stapler?
Retention Date: 07/31/2017 11:59:59 PM
Parent Folder:
Folder Display Name: Sent Items
Child Folder Count: 0
Folder Unread Count: 0
Folder Total Count: 27
---------------------------------------------------------
No Un-Read Items
问题
更新(S)
感谢。
答案 0 :(得分:0)
在仔细阅读对象浏览器后,我发现FolderName
属性的使用与FolderId
属性互斥,这解释了为什么我的parentFolderId.FolderName.Value
代码始终返回null。 / p>
我重新组织了我的代码来构建层次结构,并使用ParentFolderId
属性将EmailMessage
绑定到父文件夹,这样我就可以获得父文件夹{{1} } property。