I'm sending an email using UTF-8 encoding (using MailMessage.BodyEncoding). However, when I look at the mail message source, it states "Windows-1252" instead of "utf-8". As a result of this, some characters in the message are malformed.
Is this known behavior? How do I force the encoding to UTF-8?
Hi sandor,
In the beginning, when email was first being used, it was all us-ascii content. To handle different languages and character sets, different encodings must be used. The following example demonstrates sending a non us-ascii email, using the ISO-8859-1 character set as an example. The hardest part of sending non us-ascii email, is to determine the correct character set. For reference, an easy to use character set chart can be found at aspNetEmail's website, here:http://www.aspnetemail.com/charsets.aspx .
The following example demonstrates this technique.
C#
public static void NonAsciiMail()
{
//create the mail message
MailMessage mail = new MailMessage();//set the addresses
mail.From = new MailAddress("me@.mycompany.com");
mail.To.Add("you@.yourcompany.com");//set the content
mail.Subject = "This is an email";//to send non-ascii content, we need to set the encoding that matches the
//string characterset.
//In this example we use the ISO-8859-1 characterset
mail.Body = "this text has some ISO-8859-1 characters: aò??";
mail.BodyEncoding = Encoding.GetEncoding("iso-8859-1");//send the message
SmtpClient smtp = new SmtpClient("127.0.0.1");
smtp.Send(mail);}
VB.NET
Public Sub NonAsciiMail()
'create the mail message
Dim mail As New MailMessage()'set the addresses
mail.From = New MailAddress("me@.mycompany.com")
mail.To.Add("you@.yourcompany.com")'set the content
mail.Subject = "This is an email"'to send non-ascii content, we need to set the encoding that matches the
'string characterset.
'In this example we use the ISO-8859-1 characterset
mail.Body = "this text has some ISO-8859-1 characters: aò??"
mail.BodyEncoding = Encoding.GetEncoding("iso-8859-1")'send the message
Dim smtp As New SmtpClient("127.0.0.1")
smtp.Send(mail)
End Sub 'NonAsciiMail
Thanks.
Hi Jerome
I think you didn't get my question quite right. I know how to set the encoding but the point is that whenever I set it to UTF-8, it ends up as Windows-1252 when I look in the source of the test message I've sent myself.
Sandor.
0 comments:
Post a Comment