将电子邮件附件保存到Excel文件损坏文件

时间:2019-07-18 08:58:42

标签: .net excel vb.net mailmessage

我有一个System.Net.Mail.MailMessage,其中包含附件,将它们另存为文件时,所有Excel文件均已损坏,之后便无法打开它们。 它可以很好地处理PDF文件。

有人有一些适用于Excel文件的代码吗? .xlsx和.xlsm

我尝试了所示代码的不同版本,但是每次文件损坏

Dim buffer(convert.ToInt32(attachment.ContentStream.Length)) As Byte
Dim file As FileStream

attachment.ContentStream.Read(buffer, 0, buffer.Length)

file = New FileStream("C:\example.xlsx", FileMode.OpenOrCreate, FileAccess.ReadWrite)

file.Write(buffer, 0, buffer.Length)

file.Dispose()

我希望能够像使用PDF一样使用以附件形式发送的Excel文件

1 个答案:

答案 0 :(得分:0)

我不确定为什么不行,但我希望它能起作用:

Using fs = File.Create("file path here")
    attachment.ContentStream.CopyTo(fs)
End Using
相关问题