简体   繁体   中英

setting the encoding of a System.Net.Mail.MailMessage body while getting text from xml using c#

I'm trying to set the encoding of a MailMessage body so it is able to display german "umlauts" (ä, ö, ü). I get the message text from an xml file. The encoding of the xml is iso-8859-1 and also is the MailMessage body.

MailMessage msg = new MailMessage();
msg.From = new MailAddress(sFrom);
msg.To.Add(sTo);
msg.Subject = sSubject;
msg.Body = sBody;
msg.IsBodyHtml = true;
msg.BodyEncoding = Encoding.GetEncoding("iso-8859-1");

The first line of xml is this:

<?xml version="1.0" encoding="iso-8859-1"?>

I also tried UTF8 and Unicode encoding, but that doesn't help neither. The body is still unable to display umlauts.

Is there anything special I have to notice or am I just using the wrong encoding?

EDIT: It's a Windows Forms application and I'm using .NET framework 4.5.2

EDIT2: I'm using XmlSerializer to get the xml data.

XmlSerializer xSerializer = new XmlSerializer(typeof(App));

using (StreamReader srXml = new StreamReader(_sFileName))
{
    _oAppConfig = (App)xSerializer.Deserialize(srXml);
}

Note that App is a class I created for my own.

Try specifying the Encoding while reading the file such as:

XmlSerializer xSerializer = new XmlSerializer(typeof(App));

using (StreamReader srXml = new StreamReader(_sFileName, Encoding.GetEncoding("iso-8859-1")) )
{
    _oAppConfig = (App)xSerializer.Deserialize(srXml);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM