Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

email

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?

like image 387
Town Avatar asked Mar 19 '09 10:03

Town


People also ask

How do I add CC to MailMessage?

To add a CC recipient to an email message, create a MailAddress for the recipient's address and then add that object to the collection returned by the CC property.

What is MailMessage in asp net?

Instances of the MailMessage class are used to construct email messages that are transmitted to an SMTP server for delivery using the SmtpClient class. The sender, recipient, subject, and body of an email message may be specified as parameters when a MailMessage is used to initialize a MailMessage object.

What is System Net Mail?

Allows applications to send email by using the Simple Mail Transfer Protocol (SMTP). The SmtpClient type is obsolete on some platforms and not recommended on others; for more information, see the Remarks section.


1 Answers

I know this is old but in case someone else stumbles across it:

MailAddressCollection.Add can accept a comma delimited string of email addresses. MailAddressCollection.ToString Method returns a comma delimited string of the contained email addresses.

So the OP could have done this:

myMessage.To.Add(myMailAddressCollection.ToString());

An AddRange method would still be preferred.

like image 108
Sean Avatar answered Oct 03 '22 22:10

Sean