邮件附件(.csv)的.NET编码问题

Ric*_*cky 5 c# encoding

我使用以下代码发送带有中文字符的附件(.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);
Run Code Online (Sandbox Code Playgroud)

Ali*_*ini 2

我认为您需要为邮件正文对象进行额外的编码设置,如下所示:

message.BodyEncoding = UTF8
Run Code Online (Sandbox Code Playgroud)

  • Excell 在下一阶段检测内容编码方面通常也很差(保存到临时位置 - 丢失一些元数据 - 并打开)。确实,情况比以前更糟了。您可以尝试在文件开头放置 BOM 以暗示它是 UTF-8 内容。 (2认同)