I cannot use using
inside the loop because then I won't be able to send email because cannot access a closed stream
.
I cannot using(MemoryStream memoryStream = new MemoryStream()){the rest of the codes}
because then only first excel will have data, the rest will be empty with file size of 64 B. I already verify that all excel have data before send it via email.
foreach (workbook excel in workbooks)
{
MemoryStream memoryStream = new MemoryStream();
excel.hssfWorkBook.Write(memoryStream);
memoryStream.Position = 0;
mailMessage.Attachments.Add(new Attachment(memoryStream, excel.fileName, "application/vnd.ms-excel"));
}
smtpClient.Send(mailMessage);