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