Monday, April 25, 2011

Why is the To property of .net's MailMessage class read-only?

I've got a MailAddressCollection that contains all the addresses I want to send an email to, but instead of being able to simply have:

myMessage.To = myMailAddressCollection;

I have to do:

foreach (MailAddress address in myMailAddressCollection)  
{  
    myMessage.To.Add(address);  
}

Can anyone shed any light on why the class is configured like that? Am I missing some other way of being able to assign to the To, CC or Bcc properties?

From stackoverflow
  • The MailMessage class wants to protect it's properties, that's proper Encapsulation.

    Your task would have been a little easier if it had supported a AddRange method but as it stands your code is about as easy as it's going to get.

    Town : Yeah, AddRange would have been ideal... oh well, I guess it's not the end of the world!

0 comments:

Post a Comment