Get unread messages from pst inbox folder

Dear Support,


How i can manage to get only unread messages from inbox… i have used the below code, but it returns all messages dated old to new emails…

Dim pst As PersonalStorage = PersonalStorage.FromFile(“G:\myoutlookfile\outlook_12_11_2012.pst”)
’ Get the Display Name of the PST file
Console.WriteLine("Display Name: " & pst.DisplayName)

Dim inbox As FolderInfo = pst.RootFolder.GetSubFolder(“Inbox”)
For Each mapMsg As MapiMessage In inbox.EnumerateMapiMessages()
Console.WriteLine("Subject: " & mapMsg.Subject)
Console.WriteLine("Sender: " & mapMsg.SenderName)
Console.WriteLine("Recipients: " & mapMsg.DisplayTo)
Next

thanks

Why no one reply to my question, is not supported by the current class i am using to get unread messages??

Hi Bassam,


Thanks for writing to Aspose.Email support team.

I would like to share that MapiMessageFlags.MSGFLAG_READ can be used to retrieve the unread messages from PST. You can also get the count of unread items in a particular PST folder by using FolderInfo.ContentUnreadCount. Please give a try to the following code which displays the unread count and then all the unread messages from the Inbox of PST.

Sub Main()
Try
Console.WriteLine(“Loading PST file…”)
’ load the Outlook PST file
Dim pst As PersonalStorage = PersonalStorage.FromFile(“Sample.pst”)
’ get the Display Name of the PST file
Console.WriteLine("Display Name: " + pst.DisplayName)
’ get the folders and messages information
Dim folderInfo As FolderInfo = pst.RootFolder
’ call the recursive method to display the folder contents
DisplayFolderContents(folderInfo, pst)
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
Private Sub DisplayFolderContents(ByVal folderInfo As FolderInfo, ByVal pst As PersonalStorage)
If folderInfo.DisplayName = “Inbox” Then
’ display the folder name
Console.WriteLine(“Folder: " & Convert.ToString(folderInfo.DisplayName))
Console.WriteLine(“Unread count = " & Convert.ToString(folderInfo.ContentUnreadCount))
Console.WriteLine(”==================================”)
’ display information about messages inside this folder
Dim messageInfoCollection As MessageInfoCollection = folderInfo.GetContents()
For Each messageInfo As MessageInfo In messageInfoCollection
Dim mapi As MapiMessage = pst.ExtractMessage(messageInfo)
Dim isread As Boolean = (mapi.Flags And MapiMessageFlags.MSGFLAG_READ) = MapiMessageFlags.MSGFLAG_READ
If Not isread Then
Console.WriteLine("Subject: " + messageInfo.Subject)
Console.WriteLine(“Sender: " + messageInfo.SenderRepresentativeName)
Console.WriteLine(“Recipients: " + messageInfo.DisplayTo)
Console.WriteLine(”------------------------------”)
End If
Next
End If
’ call this method recursively for each subfolder
If folderInfo.HasSubFolders = True Then
For Each subfolderInfo As FolderInfo In folderInfo.GetSubFolders()
DisplayFolderContents(subfolderInfo, pst)
Next
End If
End Sub

Please feel free to write us back if you have any other query related to Aspose.Email.

Thank you very much for reply.


The solution above give me a good step for what I am looking for…but I think looping thru all messages in my inbox takes a lot of time because it contains above 2000 messages and only 2 unread messages out of them.

The loop alwasys starts from the old messages, is there a way to as filter for the collection to bring only the unread messages?

On more question, after read the message, how I can set the message as read.

Thank you & kind regards,
Bassam

Hi Bassam,


I would like to share with you that we already have a ticket logged in our issue tracking system for providing search facility through the PST folders. I have linked this thread with the logged ticket so as you’ll be notified once the feature is implemented. However, please note that implementation of this feature may take some time as we have other priority issues in implementation phase currently. The issue id is: NETWORKNET-33388 for your kind reference.

Hi Bassam,


In addition to above information I would like to share that as search facility is under consideration by development team, you may please consider the following as an alternative:

FolderInfo inbox = pst.RootFolder.GetSubFolder(“Inbox”);
MessageInfoCollection messages = inbox.GetContents(10, 100);

This sample code provides the facility to get selected contents from PST based upon "Start Index" and count. This option can be used to retrieve only those messages which are not processed yet based upon "Index" information saved in local file or some DB. It will help to avoid those records which are already processed.

Similarly I could not find any option to change IsRead flag of messages in the Inbox. However as workaround you may consider the following code in-conjunction with the above mentioned access of selective contents using Start Index and count. This code reads Unread messages from inbox and then deletes the original message from PST. After processing, same message is updated and its MSGFLAG_READ is updated and saved back to PST.
static private void UpdatePstMessages()
{
PersonalStorage pst = PersonalStorage.FromFile("TestInbox.pst", true);
FolderInfo infoInbox = pst.GetPredefinedFolder(StandardIpmFolder.Inbox);
MessageInfoCollection messageInfoCollection = infoInbox.GetContents(10,100);
foreach (MessageInfo messageInfo in messageInfoCollection)
{
MapiMessage mapi = pst.ExtractMessage(messageInfo);
bool isread = (mapi.Flags & MapiMessageFlags.MSGFLAG_READ) == MapiMessageFlags.MSGFLAG_READ;
if (!isread)
{
infoInbox.DeleteChildItem(messageInfo.EntryId);
mapi.SetMessageFlags(MapiMessageFlags.MSGFLAG_READ);
infoInbox.AddMessage(mapi);
}
}
}

You may please give a try to the above mentioned code and test against your scenario. Please feel free to write us back if you have any other query related to Aspose.Email.

Hi Mr. Dashif,


I have used your code above to resolve my problem, so I am waiting for updating from your side.

For update the unread to read messages it works perfect in the same folder but when I delete the message from (“inbox”) then make it as read then I add that message to another folder so gives errors for the inbox folder as says:
Cannot display the folder. Errors have been detected in the file “g:\xxx.pst”

So I can’t see the inbox folder. then I repiared my out look pst file using pstskan utitlity.
How to repair personal folder file (.pst) - Outlook | Microsoft Learn


Hi Bassam,


Could you please give a try to the following code which have two functions. First function which performs the followings steps:
  1. Create a PST
  2. Create folders Inbox and TestFolder
  3. Add 100 unread messages to the Inbox folder
Second function performs the following steps:
  1. Open the PST
  2. Select Inbox folder
  3. Read messages from 10 to 100 index (just to demonstrate selection of messages)
  4. Delete message from inbox
  5. Mark it is as Read
  6. Save the message in a temporary list
  7. Perform above steps (4 to 6) for all the messages
At end the PST is closed and opened again for adding messages in the list to the TestFolder.

static void Main(string[] args)
{
var license3 = new Aspose.Email.License();
license3.SetLicense(“Aspose.Email.lic”);

AddMessageToInbox();
UpdatePstMessages();

Console.WriteLine(“Press any key to continue…”);
Console.ReadKey();
}

static private void AddMessagsToInbox()
{
if (File.Exists(@“TestInbox.pst”))
{
File.Delete(@“TestInbox.pst”);
}
PersonalStorage pst = PersonalStorage.Create(“TestInbox.pst”, FileFormatVersion.Unicode);
pst.CreatePredefinedFolder(“Inbox”, StandardIpmFolder.Inbox);
pst.RootFolder.AddSubFolder(“TestFolder”);
MapiMessage msg = new MapiMessage("newcustomeronnet@gmail.com", "newcustomeronnet1@gmail.com", “Test Subject”, “Test Body”);
FolderInfo infoInbox = pst.GetPredefinedFolder(StandardIpmFolder.Inbox);
for (int iCount = 0; iCount <= 100; iCount++)
{
msg.Subject = “Test Subject-” + iCount;
msg.SetBodyContent(“Test Body-” + iCount, BodyContentType.PlainText);
msg.SetMessageFlags(~MapiMessageFlags.MSGFLAG_READ);
infoInbox.AddMessage(msg);
}
}
static private void UpdatePstMessages()
{
PersonalStorage pst = PersonalStorage.FromFile(“TestInbox.pst”, true);
FolderInfo infoInbox = pst.GetPredefinedFolder(StandardIpmFolder.Inbox);
FolderInfo infoTestFolder = pst.RootFolder.GetSubFolder(“TestFolder”);
List mapiMsgs = new List();
MessageInfoCollection messageInfoCollection = infoInbox.GetContents(10,100);
foreach (MessageInfo messageInfo in messageInfoCollection)
{
MapiMessage mapi = pst.ExtractMessage(messageInfo);
bool isread = (mapi.Flags & MapiMessageFlags.MSGFLAG_READ) == MapiMessageFlags.MSGFLAG_READ;
if (!isread)
{
infoInbox.DeleteChildItem(messageInfo.EntryId);
mapi.SetMessageFlags(MapiMessageFlags.MSGFLAG_READ);
mapiMsgs.Add(mapi);
}
}
pst.Dispose();
pst = PersonalStorage.FromFile(“TestInbox.pst”, true);
infoTestFolder = pst.RootFolder.GetSubFolder(“TestFolder”);
foreach (MapiMessage mapiMsg in mapiMsgs)
{
infoTestFolder.AddMessage(mapiMsg);
}
}

Please feel free to write us back if you have any other query in this regard.

Yes. It works now perfect.


Thank you so much for experiencing your product for email so far and thank you for your good support.

Hi Bassam,


Thank you for the feedback and please let us know if you have any additional query/inquiry related to Aspose.Email. We’ll be glad to assist you further.

The issues you have found earlier (filed as NETWORKNET-33388) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.