Home > Code > C# > How to Delete Messages in Bulk or One by One from Outlook PST in .NET Apps

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

by sher azam   on Jun 08, 2016   Category: C#   |  Views: 356    |  Points: 25   |  Gold 

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. To delete messages from the Sent subfolder of a previously loaded or created PST file:

• Create an instance of the FolderInfo class and load it with the contents of the Sent subfolder.
• Delete messages from the Sent folder by calling the FolderInfo.DeleteChildItem() method and passing the MessageInfo.EntryId as a parameter.

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
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
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 = "someuser@domain.com"
PersonalStorageQueryBuilder queryBuilder = new PersonalStorageQueryBuilder();
MessageInfoCollection messages = inbox.GetContents(queryBuilder.GetQuery());
IList<string> deleteList = new List<string>();
foreach (MessageInfo messageInfo in messages)

// delete messages having From = "someuser@domain.com"

//[VB.NET Code Sample]

Using pst As PersonalStorage = PersonalStorage.FromFile("test.pst")
Dim inbox As FolderInfo = pst.RootFolder.GetSubFolder("Inbox")

' find messages having From = "someuser@domain.com"
Dim queryBuilder As New PersonalStorageQueryBuilder()
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

' delete messages having From = "someuser@domain.com"
End Using

Overview: Aspose.Email for .NET

Aspose.Email for .NET is a set of components allowing developers to easily implement email functionality within their ASP.NET web applications, web services & Windows applications. It Supports Outlook PST, EML, MSG & MHT formats. It allows developers to work with SMTP, POP3, FTP & MS Exchange servers. It supports mail merge, iCalendar, customized header & body, header information, embedded files, Twitter & many more. It makes it easy to work with HTML or plain text emails & their attachments.

More about Aspose.Email for .NET

- Homepage of Aspose.Email for .NET: http://www.aspose.com/.net/email-component.aspx

- Download Aspose.Email for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.email-for-.net/default.aspx

Post Code  |  Code Snippet Home

User Responses

No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts