System.Net.Mail FAQ

Complete FAQ Listing


3.2.4 How do I create a friendly non-ascii display name? Printer Friendly

When we want to use non-ascii display names we have to specify an Encoding that matches the characterset of the name (string). This is something that needs to be known at the time of development.

Below is an example of creating a friendly display name that uses ISO-8859-1 characters.

[ C# ]

static void FriendlyNonAsciiName()
{
//create the mail message
MailMessage mail = new MailMessage();

//set the addresses
//to specify a friendly non ascii name, we use a different ctor. 
//A ctor that accepts an encoding that matches the text of the name
mail.From = new MailAddress("me@mycompany.com", "Steve Øbirk", Encoding.GetEncoding( "iso-8859-1"));
mail.To.Add("you@yourcompany.com");

//set the content
mail.Subject = "This is an email";
mail.Body = "this is the body content of the email.";

//send the message
SmtpClient smtp = new SmtpClient("127.0.0.1");
smtp.Send(mail);

}



[ VB.NET ]
Sub FriendlyNonAsciiName()
'create the mail message
Dim mail As New MailMessage()

'set the addresses
'to specify a friendly non ascii name, we use a different ctor. 
'A ctor that accepts an encoding that matches the text of the name
mail.From = New MailAddress("me@mycompany.com", "Steve Øbirk", Encoding.GetEncoding("iso-8859-1"))
mail.To.Add("you@yourcompany.com")

'set the content
mail.Subject = "This is an email"
mail.Body = "this is the body content of the email."

'send the message
Dim smtp As New SmtpClient("127.0.0.1")
smtp.Send(mail)
End Sub 'FriendlyNonAsciiName