article

How to Delete Messages from Outlook PST File inside .NET Apps

Email
Submitted on: 6/8/2016 6:59:53 AM
By: Sherazam  
Level: Intermediate
User Rating: Unrated
Compatibility: C#, VB.NET
Views: 2256
 
     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.

 
				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
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 = "someuser@domain.com" 
PersonalStorageQueryBuilder queryBuilder = new PersonalStorageQueryBuilder();
queryBuilder.From.Contains("someuser@domain.com");
MessageInfoCollection messages = inbox.GetContents(queryBuilder.GetQuery());
IList deleteList = new List();
foreach (MessageInfo messageInfo in messages)
{
deleteList.Add(messageInfo.EntryIdString);
}
// delete messages having From = "someuser@domain.com"
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 = "someuser@domain.com" 
	Dim queryBuilder As New PersonalStorageQueryBuilder()
	queryBuilder.From.Contains("someuser@domain.com")
	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 = "someuser@domain.com"
	inbox.DeleteChildItems(deleteList)
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


Other 44 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.