When retrieving a portion of the contents of a folder using start offset and count, an exception is thrown when starting at certain offsets but not others. The failing offset is determined to be a valid offset, and the email in question can be accessed by starting at a different offset and iterating up to it. Other emails immediately after the failing offset also work correctly.
I’m not sure how to upload the sample PST in question but I am happy to do that if you can show me how. It is part of one of the Enron data sets.
Below is a sample program which demonstrates the issue:
class Program
{
static void Main(string[] args)
{
InitLicense();
string inputFile = @"C:\Temp\mary_fischer_000_1_1.pst";
ReadChunks(inputFile);
}
public static void ReadChunks(string filePath)
{
// load the Outlook PST file
PersonalStorage pst = PersonalStorage.FromFile(filePath);
// Find the folder in question
var folder = FindFolder(pst.RootFolder, "Notes inbox");
if (folder == null)
return;
// Get a "chunk" of the folder contents
ReadChunk(folder, 350, 408); // This succeeds
ReadChunk(folder, 388, 408); // This succeeds
ReadChunk(folder, 365, 408); // This throws ArgumentOutOfRangeException: Non-negative number required. Parameter name: srcOffset
}
private static void ReadChunk(FolderInfo parentFolder, int startOffset, int endOffset)
{
int folderTotalCount = parentFolder.ContentCount; // This shows 409 items in the folder
int count = endOffset > folderTotalCount
? folderTotalCount - startOffset
: endOffset - startOffset;
MessageInfoCollection items = parentFolder.GetContents(startOffset, count);
}
private static FolderInfo FindFolder(FolderInfo parentFolder, string folderToFind)
{
if (parentFolder.DisplayName == folderToFind)
return parentFolder;
FolderInfoCollection subFolders = parentFolder.GetSubFolders();
if (subFolders.Count == 0)
return null;
foreach (FolderInfo subFolder in subFolders)
{
var folder = FindFolder(subFolder, folderToFind);
if (folder != null)
return folder;
}
return null;
}
private static void InitLicense()
{
new Aspose.Email.License().SetLicense("Aspose.Total.lic");
}
}