Ejemplo n.º 1
0
        private void BtnClearInvitation_Click(object sender, Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs e)
        {
            Func<string, bool> f = s => s.StartsWith("Accepted: ") || s.StartsWith("Declined: ") || s.StartsWith("Tentatively Accepted: ");

            InvitationsCounter = 0;

            Outlook.MAPIFolder inbox = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
            for (int i = inbox.Items.Count; i > 0; i--)
            //foreach (var item in inbox.Items)
            {
                var item = inbox.Items[i];
                Outlook.MailItem mail = item as Outlook.MailItem;
                if (mail != null && mail.Subject != null)
                {
                    //todo: move it to configuration
                    if (f(mail.Subject))
                    {
                        mail.Delete();
                        InvitationsCounter++;
                    }
                }
                Outlook.MeetingItem meeting = item as Outlook.MeetingItem;
                if (meeting != null)
                {
                    if (f(meeting.ConversationTopic))
                    {
                        meeting.Delete();
                        InvitationsCounter++;
                    }
                }
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Delete all mail items in folder
 /// </summary>
 /// <param name="mapiFolder">The folder need to delete all mails</param>
 public static void DeleteAllItemInMAPIFolder(Outlook.MAPIFolder mapiFolder)
 {
     if (mapiFolder.Items != null)
     {
         int count = mapiFolder.Items.Count;
         if (count == 0)
         {
             return;
         }
         else
         {
             try
             {
                 do
                 {
                     if (mapiFolder.Items.GetFirst() is Outlook.MailItem)
                     {
                         Outlook.MailItem outlookMail = (Outlook.MailItem)mapiFolder.Items.GetFirst();
                         if (outlookMail != null)
                         {
                             outlookMail.Delete();
                             Marshal.ReleaseComObject(outlookMail);
                             count--;
                         }
                     }
                     else if (mapiFolder.Items.GetFirst() is Outlook.PostItem)
                     {
                         Outlook.PostItem outlookPost = (Outlook.PostItem)mapiFolder.Items.GetFirst();
                         if (outlookPost != null)
                         {
                             outlookPost.Delete();
                             Marshal.ReleaseComObject(outlookPost);
                             count--;
                         }
                     }
                     else if (mapiFolder.Items.GetFirst() is Outlook.MeetingItem)
                     {
                         Outlook.MeetingItem outlookMeeting = (Outlook.MeetingItem)mapiFolder.Items.GetFirst();
                         if (outlookMeeting != null)
                         {
                             outlookMeeting.Delete();
                             Marshal.ReleaseComObject(outlookMeeting);
                             count--;
                         }
                     }
                     else if (mapiFolder.Items.GetFirst() is Outlook.AppointmentItem)
                     {
                         Outlook.AppointmentItem outlookAppointment = (Outlook.AppointmentItem)mapiFolder.Items.GetFirst();
                         if (outlookAppointment != null)
                         {
                             outlookAppointment.Delete();
                             Marshal.ReleaseComObject(outlookAppointment);
                             count--;
                         }
                     }
                 }while (count > 0);
             }
             catch (Exception e)
             {
                 throw new Exception(e.Message);
             }
         }
     }
 }