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:
Item | value | Description |
olFolderCalendar | 9 | The Calendar folder. |
olFolderConflicts
| 19
| The Conflicts folder (subfolder of Sync Issues folder). Only available for an Exchange account. |
olFolderContacts | 10 | The Contacts folder. |
olFolderDeletedItems | 3 | The Deleted Items folder. |
olFolderDrafts | 16 | The Drafts folder. |
olFolderInbox | 6 | The Inbox folder. |
olFolderJournal | 11 | The Journal folder. |
olFolderJunk | 23 | The 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. |
olFolderNotes | 12 | The Notes folder. |
olFolderOutbox | 4 | The Outbox folder. |
olFolderSentMail | 5 | The Sent Mail folder. |
olFolderServerFailures | 22 | The Server Failures folder (subfolder of Sync Issues folder). Only available for an Exchange account. |
olFolderSyncIssuesaccount. | 20 | The Sync Issues folder. Only available for an Exchange |
olFolderTasks | 13 | The Tasks folder. |
olFolderToDo | 28 | The To Do folder |
olPublicFoldersAllPublicFolders | 18 | The All Public Folders folder in the Exchange Public Folders store. Only available for an Exchange account. |
olFolderRssFeeds | 25
| The RSS Feeds folder.
|