The following code would work, whether you had 2, 20, or 200 names in your web.config.
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
// send the mail
MailMessage msg = new MailMessage();
msg.IsBodyHtml = false;
msg.From = new MailAddress(txtEmail.Text, txtName.Text);
string[] addressesToMailTo =
Globals.Settings.ContactForm.MailTo.Split((new char[] { ',', ' ' }), StringSplitOptions.RemoveEmptyEntries);
foreach (string address in addressesToMailTo)
msg.To.Add(address);
if (!string.IsNullOrEmpty(Globals.Settings.ContactFor m.MailCC))
{
string[] addressesToCCTo =
Globals.Settings.ContactForm.MailCC.Split((new char[] { ',', ' ' }), StringSplitOptions.RemoveEmptyEntries);
foreach (string address in addressesToCCTo)
msg.CC.Add(address);
}
msg.Subject = string.Format(
Globals.Settings.ContactForm.MailSubject, txtSubject.Text);
msg.Body = txtBody.Text;
new SmtpClient().Send(msg);
// show a confirmation message and reset the field
lblFeedbackOK.Visible = true;
lblFeedbackKO.Visible = false;
txtName.Text = "";
txtEmail.Text = "";
txtSubject.Text = "";
txtBody.Text = "";
}
catch (Exception)
{
lblFeedbackOK.Visible = false;
lblFeedbackKO.Visible = true;
}
}
|