0
從幾個公用文件夾中刪除大於15天的郵件項目,大約有15個特定公用文件夾。每個公用文件夾都有大約1000多個郵件項目。每週都是相同數量的物品。目前我正在獲取默認公用文件夾並循環每個子文件夾並刪除郵件。從Outlook公用文件夾中刪除郵件項目
Microsoft.Office.Interop.Outlook.Folder tempInbox = tempApp.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olPublicFoldersAllPublicFolders) as Microsoft.Office.Interop.Outlook.Folder;
**SOME Code***
foreach (Microsoft.Office.Interop.Outlook.Folder subfolder1 in subfolder.Folders)
{
if ((check those 14 subfolder names)& (subfolder1.Folders.Count > 0))
{
CheckCountries(subfolder1, sw);
}
}
CheckCountries(subfolder1, sw) -> Here I am comparing and deleting the mail items.
//Deletion part of code below
foreach (object todel in delItem)
{
DateTime d1 = DateTime.Now;
Microsoft.Office.Interop.Outlook.MailItem mailitmType = todel as Microsoft.Office.Interop.Outlook.MailItem;
if (mailitmType is Microsoft.Office.Interop.Outlook.MailItem)
{
if ((mailitmType.IsConflict != true) & (mailitmType.MessageClass.Contains("IPM.Note.SMIME") == false))
{
DateTime d2 = mailitmType.ReceivedTime;
if ((d1 - d2).TotalDays > iDays)
{
sw.WriteLine("Deleting Mail with Subject line as = \"" + mailitmType.Subject + "\" and Received time = " + mailitmType.ReceivedTime);
mailitmType.Delete();
iCnt = iCnt + 1;
} //mailitmType.Save();
}
}
}
我想提高在以下幾個方面 -
- 它需要近5-7小時,因爲它通過讀取所有 mailitems執行這個(如果有2000個,其中僅1000 > 15天)在每個 的15個文件夾中比較郵件的年齡,然後刪除。
- 某些文件夾由於訪問問題而失敗。所以我需要在可以訪問所有這些公共文件夾並且可以用來刪除的代碼開始處添加一個id。目前它採用運行可執行文件的默認ID。通過一個文件夾中的所有項目
感謝德米特里的建議。我厭倦了使用Items.Find/FindNext作爲下面的代碼,但項目沒有得到承認。你可以請建議在什麼可能是錯誤的下面的代碼..我得到的異常'resultItem.ReceivedTime \t'((Microsoft.Office.Interop.Outlook._MailItem)(resultItem))'爲空\t System.DateTime' – Arund
Microsoft.Office.Interop.Outlook.MailItem resultItem = delItem as Microsoft.Office.Interop.Outlook.MailItem; resultItem = null;如果(delItem.Count> 0) {string filter =「(delItem.ReceivedTime - DateTime.Now).TotalDays> 15」; resultItem = delItem.Find(filter); (resultItem!= null){ (resultItem is Microsoft.Office.Interop.Outlook.MailItem) {iCnt ++; sw.WriteLine(「刪除主題行的郵件爲= \」「+ resultItem.Subject +」\「和Received time =」+ resultItem.ReceivedTime); //// resultItem.Delete(); } Marshal.ReleaseComObject(resultItem); resultItem = delItem.FindNext(); – Arund
這不是一個有效的條件。你需要像「[ReceivedTime]> '10/7/2016'」 –