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

发布于 2024-10-04 23:11:00 字数 590 浏览 1 评论 0原文

我使用以下代码发送带有中文字符的附件(.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫九 2024-10-11 23:11:00

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

message.BodyEncoding = UTF8

i think you need an extra encoding setup for the mail body object, something like this:

message.BodyEncoding = UTF8
何以心动 2024-10-11 23:11:00

将附件编码设置为 Encoding.UTF32 可能会有所帮助。假设 attachText 是一个 string 并且您只想添加 .csv,您可以使用 Attachment.CreateAttachmentFromString() 方法:

var attachment = Attachment.CreateAttachmentFromString(attachText,
                                                       "filename.csv",
                                                       Encoding.UTF32,
                                                       "text/csv");
message.Attachments.Add(attachment);

Setting the attachments encoding to Encoding.UTF32 could help. Assuming attachText is a string and you only want to add a .csv you could shorten your code by using the Attachment.CreateAttachmentFromString() method:

var attachment = Attachment.CreateAttachmentFromString(attachText,
                                                       "filename.csv",
                                                       Encoding.UTF32,
                                                       "text/csv");
message.Attachments.Add(attachment);
苏别ゝ 2024-10-11 23:11:00

我找到了一些解决方案:

        byte[] bom = new byte[] { (byte)0xEF, (byte)0xBB, (byte)0xBF };
        var csvStr = Encoding.UTF8.GetString(bom) + csv.ToString();

对我来说效果很好

I found some solution:

        byte[] bom = new byte[] { (byte)0xEF, (byte)0xBB, (byte)0xBF };
        var csvStr = Encoding.UTF8.GetString(bom) + csv.ToString();

Works fine for me

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文