邮件附件 (.csv) 的 .NET 编码问题
我使用以下代码发送带有中文字符的附件(.csv)。但是,MS excel 无法正确显示字符,它显示类似“??”的内容。是不是因为我发送邮件时没有设置某些属性?
感谢您的任何建议。
byte[] attachBytes = Encoding.UTF8.GetBytes(attachText);
ms.Write(attachBytes, 0, attachBytes.Length);
// Set the position to the beginning of the stream.
ms.Position = 0;
ContentType ct = new ContentType(MediaTypeNames.Text.Plain);
Attachment attactment = new Attachment(ms, attachFileName);
message.Attachments.Add(attactment);
I use following code to send a attachment (.csv) with Chinese characters. However, MS excel fails to disply the characters properly, it displays something like "¨å¯¹æ–°ç". Is it because I didn't set some property when sending the mail?
Thanks for any advise.
byte[] attachBytes = Encoding.UTF8.GetBytes(attachText);
ms.Write(attachBytes, 0, attachBytes.Length);
// Set the position to the beginning of the stream.
ms.Position = 0;
ContentType ct = new ContentType(MediaTypeNames.Text.Plain);
Attachment attactment = new Attachment(ms, attachFileName);
message.Attachments.Add(attactment);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您需要为邮件正文对象进行额外的编码设置,如下所示:
i think you need an extra encoding setup for the mail body object, something like this:
将附件编码设置为
Encoding.UTF32
可能会有所帮助。假设attachText
是一个string
并且您只想添加 .csv,您可以使用Attachment.CreateAttachmentFromString()
方法:Setting the attachments encoding to
Encoding.UTF32
could help. AssumingattachText
is astring
and you only want to add a .csv you could shorten your code by using theAttachment.CreateAttachmentFromString()
method:我找到了一些解决方案:
对我来说效果很好
I found some solution:
Works fine for me