.Net System.Mail.Message adding multiple "To" addresses

Posted by Matt Dawdy on Stack Overflow See other posts from Stack Overflow or by Matt Dawdy
Published on 2010-03-08T20:42:17Z Indexed on 2010/03/08 20:51 UTC
Read the original article Hit count: 281

Filed under:
|

I just hit something I think is inconsistent, and wanted to see if I'm doing something wrong, if I'm an idiot, or...

MailMessage msg = new MailMessage();
msg.To.Add("[email protected]");
msg.To.Add("[email protected]");
msg.To.Add("[email protected]");
msg.To.Add("[email protected]");

Really only sends this email to 1 person, the last one.

To add multiples I have to do this: msg.To.Add("[email protected],[email protected],[email protected],[email protected]");

I don't get it. I thought I was adding multiple people to the "To" address collection, but what I was doing was replacing it.

I think I just realized my error -- to add one item to the collection, use .To.Add(new MailAddress("[email protected]"))

If you use just a string, it replaces everything it had in its list.

Ugh. I'd consider this a rather large gotcha! Since I answered my own question, but I think this is of value to have in the stackoverflow archive, I'll still ask it. Maybe someone even has an idea of other traps that you can fall into.

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about system.net.mail