如何在AlternateView上设置编码

时间:2010-11-15 14:39:33

标签: c# .net-3.5

我正在通过

创建一个mailBody
using (var streamWriter = new StreamWriter(fooFileName, false, Encoding.UTF8))
{
    streamWriter.Write(fooMessage);
}

现在我正在使用

来使用此文件
var mailMessage = new System.Net.Mail.MailMessage
{
    From = fooSender,
    To =
    {
        fooRecipient
    },
    Subject = fooSubject,
    SubjectEncoding = Encoding.UTF8,
    BodyEncoding = Encoding.UTF8
};
// fooMediaType can be
// - System.Net.Mime.MediaTypeNames.Text.Html
// - System.Net.Mime.MediaTypeNames.Text.Plain
var alternateView = new System.Net.Mail.AlternateView(fooFileName, fooMediaType);
mailMessage.AlternateViews.Add(alternateView);

如果我使用非变音符号,一切正常,但只要我在alternateView中使用变音符号,它们就会被破坏。

那么如何在alternateView上设置某种编码,以确保一切正常?

1 个答案:

答案 0 :(得分:16)

 var alternateView = new AlternateView(fooFileName, fooMediaType)
 {
      ContentType =
      {
           CharSet = Encoding.UTF8.WebName
      }
 };