System.Net.Mail FAQ

Complete FAQ Listing


3.2.1 How do I change the FROM address to a friendly name? Printer Friendly

Because the MailMessage.From property is a MailAddress, we simply need to use a different ctor to create a MailAddress. The ctor we will use will accept a friendly display name.

[ C# ]

static void FriendlyFromName()
{ 
//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" );
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 FriendlyFromName()
'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")
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 'FriendlyFromName