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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//your code here...//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");
X

Url: http://www.aspose.com/.net/email-component.aspx

Language: C# | User: Sheraz Khan | Created: Jun 8, 2016 | Tags: Delete Messages from PST Files Delete Items in Bulk from PST Delete Items from PST file one by one add messages to PST file Outlook PST to MSG C#, .NET Outlook API