<<Back To Faq 3.2.2


Complete FAQ Listing


3.2.2 How do I change the TO address to a friendly name? Printer Friendly

Chainging the To, CC, and Bcc addresses to use a Friendly display name, is just like we did for the .From property. We simply need to use a different MailAddress ctor to achieve this functionality.

Below is a sample that demonstrates this.

[ C# ]

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

//set the addresses
//to specify a friendly 'from' name, we use a different ctor
mail.From = new MailAddress("me@mycompany.com", "Steve James");

//since the To,Cc, and Bcc accept addresses, we can use the same technique as the From address
mail.To.Add( new MailAddress( "you@yourcompany.com", "Beth Jones") );
mail.CC.Add(new MailAddress("donna@yourcompany.com", "Donna Summers"));
mail.Bcc.Add(new MailAddress("bob@yourcompany.com", "Bob Smith"));

//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 FriendlyToName()
'create the mail message
Dim mail As New MailMessage()

'set the addresses
'to specify a friendly 'from' name, we use a different ctor
mail.From = New MailAddress("me@mycompany.com", "Steve James")

'since the To,Cc, and Bcc accept addresses, we can use the same technique as the From address
mail.To.Add(New MailAddress("you@yourcompany.com", "Beth Jones"))
mail.CC.Add(New MailAddress("donna@yourcompany.com", "Donna Summers"))
mail.Bcc.Add(New MailAddress("bob@yourcompany.com", "Bob Smith"))

'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 'FriendlyToName



 



The formatted version of this faq can be found at http://www.SystemNetMail.com/faq/3.2.2.aspx