I'm attempting to send an email and instead of sending 5 individual emails to each person, I would like to send one mass email to all 5 people. The difference here is that I want everyone to show up in the "TO" or "CC" lines. How can I do this?
+5
A:
This will send only one message:
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress("[email protected]", "Optional Name"));
msg.To.Add(new MailAddress("[email protected]"));
msg.To.Add(new MailAddress("[email protected]"));
msg.CC.Add(new MailAddress("[email protected]"));
msg.CC.Add(new MailAddress("[email protected]"));
// can add to BCC too
// msg.Bcc.Add(new MailAddress("[email protected]"));
msg.Body = "...";
msg.Subject = "...";
SmtpClient smtp = new SmtpClient();
smtp.Send(msg);
John Sheehan
2009-05-14 21:34:09
Great example, but maybe it would help if you added using System.Net.Mail; and smtp.Host = "mail.mydomain.com"; or something of the sort so that users unfamiliar with the classes may be more successful with their implementations.
Michael La Voie
2009-05-14 21:36:41
I prefer to have all my System.Net.Mail config handled in the web.config (see this answer for why: http://stackoverflow.com/questions/54929/hidden-features-of-asp-net/75170#75170)
John Sheehan
2009-05-14 21:37:50
Silly me. I was actually doing this but I made a small mistake so I didn't realize it. Thanks.
Joe Philllips
2009-05-14 22:12:46
+2
A:
MailMessage.CC.Add(new MailAddress(address))
Check out: http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.cc.aspx
Lloyd
2009-05-14 21:34:25
+2
A:
using System.Net.Mail;
...
MailMessage mmsg = new MailMessage();
mmsg.To.Add(new MailAddress("[email protected]"));
mmsg.To.Add(new MailAddress("[email protected]"));
mmsg.To.Add(new MailAddress("[email protected]"));
// set other properties here...
SmtpClient client = new SmtpClient("mymailserver.com");
client.Send(mmsg);
Edit: aww, john's fingers are faster than mine ;)
Scott Anderson
2009-05-14 21:35:30