使用 C# 发送的电子邮件中的附件中的 zip 文件为空

zip file comming empty in attachement in email sent using c#

我正在创建一个 zip 文件并在我的项目中使用 c# 将其附加到电子邮件

我正在使用 DotNetZip。

下面是它的代码

Attachment attachment;
MemoryStream memoryStreamOfFile = new MemoryStream();
using (ZipFile zip = new ZipFile()) {
     zip.Password = "123456";
     zip.Encryption = EncryptionAlgorithm.WinZipAes256;
     zip.AddEntry(FileName + ".csv", stream);
     zip.Save(memoryStreamOfFile);
     attachment = new Attachment(memoryStreamOfFile, new ContentType("application/zip")) {Name = FileName + ".zip"};
} 

我实际上想做的是我有 byte[],我正在将其转换为 MemoryStream 并将其作为 csv 添加到 zip 并将该 zip 文件附加到电子邮件。 但是 zip 文件在电子邮件中是空的。我无法在我的驱动器中创建 zip 文件,我只能创建 MemoryStream。

我做错了什么吗?

您可能需要在保存命令后重置流的位置。

zip.AddEntry(FileName + ".csv", stream);
zip.Save(memoryStreamOfFile);
memoryStreamOfFile.Position = 0;