[英]How to list all folders and files in those folders using Exchange Web Services Managed API?
我剛剛發現了Exchange Web Services Managed API,並且一直在嘗試以了解它,因為我相信它可以為我一直在研究的問題提供解決方案。
背景
我已經閱讀了很多文檔,並對事物有一般的了解。 特別是,我設置了與此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
問題
更新)
謝謝。
仔細閱讀對象瀏覽器后,我發現FolderName
屬性與FolderId
屬性是互斥的,這解釋了為什么我的parentFolderId.FolderName.Value
代碼始終返回null的原因。
我重新組織了代碼以構建層次結構,並使用ParentFolderId
屬性將EmailMessage
綁定到父文件夾,這使我可以獲得父文件夾的DisplayName
屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.