How to delete emails from outlook through c#

How to delete emails from outlook through c#
The following example demonstrate how we can delete emails or messages from various mail folders of the Outlook through C#. In the following example we are deleting emails from Junk folder. check this:

private void DelateEmailFromOutlook()
   {
      Microsoft.Office.Interop.Outlook.Application tempApp = new
                Microsoft.Office.Interop.Outlook.Application();
      MAPIFolder tempInbox = default(MAPIFolder);
      Items JunkItems = default(Items);
 
      tempInbox = tempApp.GetNamespace("MAPI").
           GetDefaultFolder(OlDefaultFolders.olFolderJunk);
      JunkItems = tempInbox.Items;
      MailItem DeleteMail = default(MailItem);
      foreach (object newMail_loopVariable in JunkItems)
        {
           DeleteMail = (MailItem)newMail_loopVariable;
           DeleteMail.Delete();
        }
      JunkItems = null;
      tempInbox = null;
      tempApp = null;
}

from the above we are deleting all email from Junk folder, consider this line:

tempInbox = tempApp.GetNamespace("MAPI").
           GetDefaultFolder(OlDefaultFolders.olFolderJunk);

we are accessing junk folder through OlDefaultFolders.olFolderJunk, if we want to delete mail from sent items folder then we can get that folder as:

tempInbox = tempApp.GetNamespace("MAPI").
           GetDefaultFolder(OlDefaultFolders.olFolderSentMail);

we can use above code with these items also:

ItemvalueDescription
olFolderCalendar9The Calendar folder.
olFolderConflicts

 

19

 

The Conflicts folder (subfolder of     Sync Issues folder).

Only available for an Exchange account.

olFolderContacts10The Contacts folder.
olFolderDeletedItems3The Deleted Items folder.
olFolderDrafts16The Drafts folder.
olFolderInbox6The Inbox folder.
olFolderJournal11The Journal folder.
olFolderJunk23The Junk E-Mail folder.
olFolderLocalFailures

 

21

 

The Local Failures folder (subfolder of Sync Issues

folder). Only available for an Exchange account.

olFolderManagedEmail

 

 

29

 

 

The top-level folder in the Managed Folders group. For

more information on Managed Folders, see Help in Microsoft Outlook. Only  available for an  Exchange account.

olFolderNotes12The Notes folder.
olFolderOutbox4The Outbox folder.
olFolderSentMail5The Sent Mail folder.
olFolderServerFailures22The Server Failures folder (subfolder of Sync Issues  folder). Only available for an Exchange account.
olFolderSyncIssuesaccount.20The Sync Issues folder. Only  available for an Exchange
olFolderTasks13The Tasks folder.
olFolderToDo28The To Do folder
olPublicFoldersAllPublicFolders18The All Public Folders folder in the Exchange Public  Folders store. Only available for an Exchange account.
olFolderRssFeeds25

 

The RSS Feeds folder.