Tag: system.net.mail

如何closures在一个循环内创build多个MemoryStream?

我不能使用内循环,因为那样我就不能发送电子邮件,因为cannot access a closed stream 。 我不能using(MemoryStream memoryStream = new MemoryStream()){the rest of the codes}因为那么只有第一个Excel会有数据,其余的将是空的,文件大小为64 B.我已经validation所有的Excel有数据之前发送它通过电子邮件。 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);