8,582 17
Top 1% !
Pearl of Wisdom
Popularity: 9499th place

How to Delete Messages in Bulk or One by One from Outlook PST in .NET Apps

This technical tip explains how .NET developers can Delete Messages from outlook PST. Add Messages to PST Files showed how to add messages to PST files. It is, of course, also possible to delete items (contents) from a PST file and it may also be desirable to delete messages in bulk. Items from a PST file can be deleted using the FolderInfo.DeleteChildItem() method. The API also provides FolderInfo.DeleteChildItems() method to delete items in bulk from the PST file. This articles shows how to Delete Items from a PST file one by one and delete Items in Bulk from a PST file.
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
// Enter here the actual content of the snippet. //Deleting Messages from PST Files //The code snippets below delete messages from a PST file's Sent subfolder. //[C# Code Sample] // Get the Sent items folder FolderInfo folderInfo = pst.GetPredefinedFolder(StandardIpmFolder.SentItems); MessageInfoCollection msgInfoColl = folderInfo.GetContents(); foreach (MessageInfo msgInfo in msgInfoColl) { Console.WriteLine(msgInfo.Subject + ": " + msgInfo.EntryIdString); if (msgInfo.Subject.Equals("some delete condition") == true) { // Delete this item folderInfo.DeleteChildItem(msgInfo.EntryId); Console.WriteLine("Deleted this message"); } } //[VB.NET Code Sample] ' Get the Sent items folder Dim folderInfo As FolderInfo = pst.GetPredefinedFolder(StandardIpmFolder.SentItems) Dim msgInfoColl As MessageInfoCollection = folderInfo.GetContents() For Each msgInfo As MessageInfo In msgInfoColl Console.WriteLine(msgInfo.Subject & ": " & msgInfo.EntryIdString) If msgInfo.Subject.Equals("some delete condition") = True Then ' Delete this item folderInfo.DeleteChildItem(msgInfo.EntryId) Console.WriteLine("Deleted this message") End If Next msgInfo //Delete Items in Bulk from PST File //Aspose.Email API can be used to delete items in bulk from a PST file. This is achieved using the DeleteChildItems method which accepts a list of Entry ID items referring to the items to be deleted. //[C# Code Sample] using (PersonalStorage pst = PersonalStorage.FromFile(@"test.pst")) { FolderInfo inbox = pst.RootFolder.GetSubFolder("Inbox"); // find messages having From = "" PersonalStorageQueryBuilder queryBuilder = new PersonalStorageQueryBuilder(); queryBuilder.From.Contains(""); MessageInfoCollection messages = inbox.GetContents(queryBuilder.GetQuery()); IList<string> deleteList = new List<string>(); foreach (MessageInfo messageInfo in messages) { deleteList.Add(messageInfo.EntryIdString); } // delete messages having From = "" inbox.DeleteChildItems(deleteList); } //[VB.NET Code Sample] Using pst As PersonalStorage = PersonalStorage.FromFile("test.pst") Dim inbox As FolderInfo = pst.RootFolder.GetSubFolder("Inbox") ' find messages having From = "" Dim queryBuilder As New PersonalStorageQueryBuilder() queryBuilder.From.Contains("") Dim messages As MessageInfoCollection = inbox.GetContents(queryBuilder.GetQuery()) Dim deleteList As IList(Of String) = New List(Of String)() For Each messageInfo As MessageInfo In messages deleteList.Add(messageInfo.EntryIdString) Next ' delete messages having From = "" inbox.DeleteChildItems(deleteList) End Using
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus