Hello!
I have a program, that downloads attachment from an email through Aspose.Email 22.4 and parses data in it with Aspose Cells 22.4 under .Net Framework 4.5
Everything worked fine for a while, but some time ago I started to get error, while getting the workbook:
Index and count must refer to a location within the buffer. Parameter name: bytes
How can i solve it? There is no chance to change an excel file…
Code:
using (var client = EWSClient.GetEWSClient("ServerUri", credential))
{
var mailbox = client.GetMailboxInfo("SomeEmail");
var messages = client.ListMessages(mailbox.InboxUri);
var ordered = messages.OrderBy(s => s.Date);
foreach (var messageInfo in ordered)
{
var mailMessage = client.FetchMessage(messageInfo.UniqueUri);
foreach (var attachment in mailMessage.Attachments)
{
if (string.Compare(attachment.Name, "SomeFileName", StringComparison.OrdinalIgnoreCase) != 0)
continue;
//Get bytes for inline read Excel
var len = attachment.ContentStream.Length;
var buffer = new byte[len];
attachment.ContentStream.Read(buffer, 0, (int) len);
//Save file to folder
var fileNameToHistory = Path.Combine("SomePathToFolder", "SomeXLSFileName.xls");
attachment.Save(fileNameToHistory);
//Read data from attachment
using (var stream = new MemoryStream(buffer))
{
stream.Seek(0, SeekOrigin.Begin);
workbook = new Workbook(stream); // <<-- Error here
}
//Read data from saved file
using (var file = File.OpenRead(fileNameToHistory))
{
var workbook = new Workbook(file); // <<-- Error here
}
}
}
}
Could you please share a standalone sample project with resource files to reproduce the issue on our end, we will check it soon. Also, did you try using latest version of the APIs (e.g., Aspose.Email and Aspose.Cells) if it makes any difference?
PS. please zip the project and files prior attaching.
I’ve made a ConsoleApp where copied some code to reproduce error (all the config is in the Program.cs) ConsoleApp2.zip (5.7 KB)
I also attach the .xls file, that is saved from Email (unzip it) w.zip (942.1 KB)
There is a commented code in ExcelHelper.cs, that I used to resave file using Marshal. After resaving the file opens correctly
I tested your scenario/case with your sample code by providing test URL, login, pwd and other details. But when I tried to run the code, it gives error on ProcessMails() method (ProcessFile() works fine). I am not sure if the issue is with Aspose.Cells APIs by any means or with Aspose.Email or due to other configurations. Could you please separate the issue and use only Aspose.Cells to reproduce the issue on our end. If you are successful, kindly do share sample (runnable) code for it. This way, it will be confirmed that the issue is with Aspose.Cells and we can evaluate the issue to figure it out soon.
Would you please save the data in your “buffer” to a file and then load the workbook from the saved file to check whether the error can be reproduced? If so, please send us the saved file so we can trace and fix the issue.