Extract message from a folder of a ost file

MessageInfoCollection messageColl = myfolderInfo.GetContents();
i use this code to get all message from a folder of a ost file. But when the folder contains a large number of mails then this code takes more time.
Please give me some way to solve this issue.

@alibardisk

You may filter messages using QueryBuilder to avoid loading bulk message as given below:

// Emails that arrived today
MailQueryBuilder builder = new MailQueryBuilder();
builder.InternalDate.On(DateTime.Now);

// Emails that arrived in last 7 days
builder.InternalDate.Before(DateTime.Now);
builder.InternalDate.Since(DateTime.Now.AddDays(-7));

Please find more details with code sample at this link: https://docs.aspose.com/display/emailnet/Filter+Messages+From+Exchange+Mailbox

Furthermore, please feel free to write back to us if you need additional information.

but i want to get all message from the folder…

@alibardisk

You may get messages with paging support and for this purpose, please get details along with sample code snippet at this link:
https://docs.aspose.com/display/emailnet/Filter+Messages+from+Server+using+IMAP+Client#FilterMessagesfromServerusingIMAPClient-FilterMessageswithPagingSupport

Moreover, you may use the following code example as well:

ImapMessageInfoCollection messageCollection = imapClient.listMessages();
int totalMessages = messageCollection.size();
int messagesPerPage = 100;

int totalPages = (int) ceil((double) totalMessages/(double) messagesPerPage);

for(int i=1;i<=totalPages;i++){
    ImapPageInfo pageInfo = imapClient.listMessagesByPage(messagesPerPage, i-1, new PageSettings());
    ImapMessageInfoCollection messagePageCollection = pageInfo.getItems();

    for(ImapMessageInfo info : messagePageCollection){
        System.out.println("Subject: " + info.getSubject());
    }
}

You can also check whether the current page is the last page and break the loop by using the code snippet given below.

// You can use this
System.out.println(pageInfo.getNextPage()); // Will return null if the current page is the last page.
// Or you can use this
System.out.println(pageInfo.getLastPage()); // Will return true if the current page is the last

Furthermore, please feel free to write back to us if you need additional information or if you have more queries in future.

But sir i have a Offline ost file.
How can i use imapclient?

@alibardisk

In case of offline PST/OST file, you may use the following method by providing startIndex and count:

MessageInfoCollection messageInfoCollection = folderInfo.GetContents(0,100); 

The above code shall return first 100 messages. You may try the following code sample:

int totalMessages = folderInfo.ContentCount;
int messagesPerPage = 100;

int totalPages = (int)((double)totalMessages / (double)messagesPerPage);

for (int i = 1; i <= totalPages; i++)
{
    MessageInfoCollection messageInfoCollection = folderInfo.GetContents(i, 100);

    foreach (MessageInfo messageInfo in messageInfoCollection)
    {
        Console.WriteLine("Message {0} ....", messageInfo.Subject);
    }
}

We hope that this shall resolve your problem. Please provide your feedback.

Thank you sir for help me.

@alibardisk

You are welcome, please feel free to write back to us if you need additional information or if you have any queries in future.

Sir i have a ost file.when i try to get the contacts from the contact folder id shows a error message :

"An unhandled exception of type ‘System.InvalidOperationException’ occurred in Aspose.Email.dll

Additional information: Could not read messages in folder"

How can i solve this issue?
File link : jeff@fleet.works_2.ost - Google Drive

@alibardisk

Thank you for writing back to us.

We are investigating this issue and will let you know soon.

Same error occured for “Sent Items” foder in to above file.

@alibardisk

Please mention which version of API are you using.

Moreover, we have logged this issue with ID “EMAILNET-39274” for further investigation and you will automatically be informed here once we have more information to share.

Product version : 17.11

@alibardisk

Can you please try it with latest version of our API which is Aspose.Email for .NET 18.12 and share your feedback with us.

It’s work.
thank you sir for help me.

@alibardisk

You are welcome. Please feel free to write back to us for further queries.

same error occured after retrive 2425 items from contact folder and
“An unhandled exception of type ‘System.OutOfMemoryException’ occurred in Aspose.Email.dll”
error occured after retrive 2200 items from Sent Items folder of the above file.
please suggest me.

@alibardisk,

Can you please provide the sample project reproducing the issue on your end.

Project Link : https://drive.google.com/open?id=1Vt99yRtWE9Rtz5oQ4hPyoxYwAmgPaBlp

@alibardisk,

Thank you for sharing the information. We have associated the information in our issue tracking system and will share further feedback with you after completion of investigation on our end.