0 out of 3 rated this helpful - Rate this topic

MailMessage.To Property

Gets the address collection that contains the recipients of this e-mail message.

Namespace:  System.Net.Mail
Assembly:  System (in System.dll)
public MailAddressCollection To { get; }

Property Value

Type: System.Net.Mail.MailAddressCollection
A writable MailAddressCollection object.

The To property is used to designate the addresses on the To line of an e-mail message. To add a recipient to an e-mail message, create a MailAddress for the recipient's address, and then add that object to the collection returned by this property.

The following code example demonstrates setting the To property.


		public static void CreateTestMessage4(string server)
		{
			MailAddress from = new MailAddress("ben@contoso.com");
			MailAddress to = new MailAddress("Jane@contoso.com");
			MailMessage message = new MailMessage(from, to);
			message.Subject = "Using the SmtpClient class.";
			message.Body = @"Using this feature, you can send an e-mail message from an application very easily.";
			SmtpClient client = new SmtpClient(server);
			Console.WriteLine("Sending an e-mail message to {0} by using SMTP host {1} port {2}.",
				 to.ToString(), client.Host, client.Port);

      try {
			  client.Send(message);
			}
			catch (Exception ex) {
			  Console.WriteLine("Exception caught in CreateTestMessage4(): {0}", 
                    ex.ToString() );			  
		  }
		}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Multiple addresses in "To" property
$0If you want to add more addresses, divide them by comma. And the space will divide display name and email address. The "To" property accepts following formats:$0 $0 $0"email@server.com"$0 $0"email1@server1.com,  email1@server1.com"$0 $0"Name email@server.com"$0 $0"name email@server1.com, email@server2.com"$0 $0 $0etc...$0
The To Property is a Collection
You can easily modify the To property, because it is a Collection.  Use any of the Collection methods, such as Add, Remove, Clear, etc.  For more details about MailMessageCollection methods see http://msdn.microsoft.com/en-us/library/system.net.mail.mailaddresscollection.aspx

Here's an example that uses Add() to put three addresses in the To property. It also uses the Aggregate method to display the addresses.  The method is from a Form application so it uses MessageBox.Show() instead of Console.WriteLine().

        public static void CreateTestMessage5(string server)
        {
            MailMessage message = new MailMessage();
            message.Subject = "Sending to multiple recipients";
            message.Body = "Using the SmtpClient class, and adding/modifying the 'to' field after the message object is created.";
 
            string from = "foo@nomail.com";
            string to = "bar@nomail.com, baz@nomail.com, qux@nomail.com";
            string[] toAddresses = Regex.Split(to, "[,;] *");
 
            foreach (string toAddress in toAddresses)
            {
                message.To.Add(toAddress);
                // OR message.To.Add(new MailAddress(toAddress));
            }
            message.From = new MailAddress(from);
 
            SmtpClient client = new SmtpClient(server);
            try
            {
client.Send(message);                 MessageBox.Show(String.Format("Message sent to {0}by using SMTP host {1} port {2}",                     message.To.Aggregate<MailAddressstring>("", (str1, address) => str1 + address.Address + ", "),                      client.Host, client.Port));             }             catch (Exception ex)             {                 string str = String.Format("Exception caught in CreateTestMessage5(): {0} - {1}", ex.ToString(), ex.Message);                 MessageBox.Show(str);             }         }
Poor example

With all due respect, the code example doesn't show how to set the 'To' property - or how to 'get' it, for that matter.

It only shows the way to pass a 'To' address into the constructor of the MailMessage class.

Surely a more valid (and possibly additional) example would be to show how to add multiple recipients to the 'To' collection.

Why the property "set" is not allowed?
For a mass email distribution requirement, MailMessage object should be created each time instead of changing the "Receipient" email address...