我有一个 excel 文件(Excel 2003 / xls 格式),我想用 c# 通过电子邮件发送它。我的代码发送成功,但是当我尝试打开响应文件时,它似乎编码错误。
例如这里是响应文件名:
=_utf-8_B_RWxzesOhbW9sw6FzXzIwMTJfMTBfMTZf.dat
这是响应文件本身:
=?utf-8?B?VEdWdmJIWmhjMkZ1Wk1Pelh6UXlYekZmPz0NCiA9P3V0Zi04P0I/VGtW?=\ \ =?utf-8?B?TlgwZFRXaTU0YkhNPT89?=" 内容传输编码:base64 内容处置:附件
0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAAAPgADAP7/CQAGAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAEAAAAWAAAAEAAAD+////AAAAAAAAAAD//////////////////////////////////// ///////////////////////////////////////// ///////////////////// ....
这是我的代码片段:
...
var attachment = new Attachment(WriteFileToMemory("fileFullPath"), "fileName.xls");
attachment.ContentType = new ContentType("application/vnd.ms-excel");
attachmentCollection.Add(attachment);
...
private Stream WriteFileToMemory(string filePath)
{
var memoryStream = new MemoryStream();
_openedStreams.Add(memoryStream);
using (var file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
var bytes = new byte[file.Length];
file.Read(bytes, 0, (int) file.Length);
memoryStream.Write(bytes, 0, (int) file.Length);
file.Close();
}
memoryStream.Position = 0;
return memoryStream;
}
如何设置附件编码类型,以及我应该对 Excel 文件使用哪种编码?
请帮我解决这个问题。提前致谢。