Send Email to multiple Recipients with MailMessage?

Chetan Goenka picture Chetan Goenka · May 6, 2014 · Viewed 198.1k times · Source

I have multiple email recipients stored in SQL Server. When I click send in the webpage it should send email to all recipients. I have separated emails using ;.

Following is the single recipient code.

MailMessage Msg = new MailMessage();
MailAddress fromMail = new MailAddress(fromEmail);
Msg.From = fromMail;
Msg.To.Add(new MailAddress(toEmail));

if (ccEmail != "" && bccEmail != "")
{
    Msg.CC.Add(new MailAddress(ccEmail));
    Msg.Bcc.Add(new MailAddress(bccEmail));
}

SmtpClient a = new SmtpClient("smtp server name");
a.Send(Msg);
sreader.Dispose();

Answer

Brendan Green picture Brendan Green · May 6, 2014

Easy!

Just split the incoming address list on the ";" character, and add them to the mail message:

foreach (var address in addresses.Split(new [] {";"}, StringSplitOptions.RemoveEmptyEntries))
{
    mailMessage.To.Add(address);    
}

In this example, addresses contains "[email protected];[email protected]".