0

There are about 15 particular public folders out of several public folders from which I am deleting mailitems which are greater than 15 days old. Each public folder has around 1000+ mailitems. Every week it's the same quantity of items. Currently I am getting the default public folder and looping each of the sub-folders and deleting the mails.

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();
    }
}
}

I want to improve on the following areas -

  • It takes almost 5-7 hours to execute this as it reads through all the mailitems (if there are 2000 of which only 1000 are > 15 days) in each of the 15 folders and compares the age of the mail and then deletes.
  • Some of the folders fails due to access issue. So I need to add an id at the start of the code which has access to all of these public folders and can be used to delete. Currently it takes the default id that is running the executable.
Community
  • 1
  • 1
Arund
  • 1

1 Answers1

0

Never loop through all item in a folder - use Items.Find/FindNext of Items.Restrict with a query on ReceivedTime property being less than some value.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • Thanks Dmitry for the suggestion. I tired using the Items.Find/FindNext as below code but items is not getting recognized. Could you please suggest on what might be wrong with the below code.. I get the exception as 'resultItem.ReceivedTime '((Microsoft.Office.Interop.Outlook._MailItem)(resultItem))' is null System.DateTime' – Arund Oct 07 '16 at 15:08
  • Microsoft.Office.Interop.Outlook.MailItem resultItem = delItem as Microsoft.Office.Interop.Outlook.MailItem;resultItem = null; if (delItem.Count > 0) {string filter = "(delItem.ReceivedTime - DateTime.Now).TotalDays> 15"; resultItem = delItem.Find(filter); while (resultItem != null){ (resultItem is Microsoft.Office.Interop.Outlook.MailItem) { iCnt++; sw.WriteLine("Deleting Mail with Subject line as = \"" + resultItem.Subject + "\" and Received time = " + resultItem.ReceivedTime); ////resultItem.Delete(); }Marshal.ReleaseComObject(resultItem);resultItem = delItem.FindNext(); – Arund Oct 07 '16 at 15:12
  • It is not a valid condition. You need something like "[ReceivedTime] > '10/7/2016'" – Dmitry Streblechenko Oct 07 '16 at 15:24