Help Sending Email using ASP.NET and C# to Multiple Recipients

I am having some trouble with the syntax. I’m trying to build a web form that users (at my work) can enter multiple email addresses into. For some reason it will only send to one email address.

Here’s my code… Please help. Hopefully all I need to do is change the syntax a bit.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net.Mail;

public partial class SendEmail1 : System.Web.UI.Page
{
string FileNameToAttache;

protected void Page_Load(object sender, EventArgs e)
{

}
protected void sendButton_Click(object sender, EventArgs e)
{
    SmtpClient smtpClient = new SmtpClient();
    MailMessage message = new MailMessage();
    try
    {
        MailAddress fromAddress = new MailAddress(fromTextBox.Text);

        MailAddress toAddress = new MailAddress(toTextBox.Text, "");
        message.From = fromAddress;
        message.To.Add(toAddress);
        message.Subject = subjectTextBox.Text;
        message.IsBodyHtml = true;

/* Attaching Files Begin */
if (AttachFile1.PostedFile != null)
{

        HttpPostedFile attFile = AttachFile1.PostedFile;
        int attachFileLength = attFile.ContentLength;
        if (attachFileLength > 0)
        {
            FileNameToAttache = Path.GetFileName(AttachFile1.PostedFile.FileName);
            AttachFile1.PostedFile.SaveAs(Server.MapPath(FileNameToAttache));
            message.Attachments.Add(new Attachment(Server.MapPath(FileNameToAttache)));
        }
    }

    SmtpClient emailWithAttach = new SmtpClient();
    emailWithAttach.Host = "DEVSMTP.corp.perfect-10.tv";
    emailWithAttach.DeliveryMethod = SmtpDeliveryMethod.Network;
    emailWithAttach.UseDefaultCredentials = true;

    if (AttachFile2.PostedFile != null)
    {

        HttpPostedFile attFile = AttachFile2.PostedFile;
        int attachFileLength = attFile.ContentLength;
        if (attachFileLength > 0)
        {
            FileNameToAttache = Path.GetFileName(AttachFile2.PostedFile.FileName);
            AttachFile1.PostedFile.SaveAs(Server.MapPath(FileNameToAttache));
            message.Attachments.Add(new Attachment(Server.MapPath(FileNameToAttache)));
        }
    }
    if (AttachFile3.PostedFile != null)
    {

        HttpPostedFile attFile = AttachFile3.PostedFile;
        int attachFileLength = attFile.ContentLength;
        if (attachFileLength > 0)
        {
            FileNameToAttache = Path.GetFileName(AttachFile3.PostedFile.FileName);
            AttachFile1.PostedFile.SaveAs(Server.MapPath(FileNameToAttache));
            message.Attachments.Add(new Attachment(Server.MapPath(FileNameToAttache)));
        }
    }
    if (AttachFile4.PostedFile != null)
    {

        HttpPostedFile attFile = AttachFile4.PostedFile;
        int attachFileLength = attFile.ContentLength;
        if (attachFileLength > 0)
        {
            FileNameToAttache = Path.GetFileName(AttachFile4.PostedFile.FileName);
            AttachFile1.PostedFile.SaveAs(Server.MapPath(FileNameToAttache));
            message.Attachments.Add(new Attachment(Server.MapPath(FileNameToAttache)));
        }
    }

/* Attaching Files END */

        message.Body =
            "<html><head><title>" + HttpUtility.HtmlEncode(subjectTextBox.Text) +
            "</title></head><body style='background-color:#edf8ff;'><div style='width:600px;'><table width='600' align='center' style='background-color:#ffffff;'>" + "<img src=\\"http:www.perfect-vision.com/Images/FlashReplacement.jpg\\" />" +
            "<tr><td colspan='2'style='width:500px;'><p>" + HttpUtility.HtmlEncode(greetingTextBox.Text) + "</p></td></tr><br />" +
            "<tr><td colspan='2'style='width:500px;'><p>" + HttpUtility.HtmlEncode(createTextBox.Text) + "</p></td><img src=\\"http:www.perfect-vision.com/Images/UL_SCTE.gif\\" /></tr><br />" +
            "<tr><td colspan='2'style='width:500px;'><p>" + HttpUtility.HtmlEncode(closingTextBox.Text) + "</p></td></tr>" +
            "<tr><td><img src=\\"http:www.perfect-vision.com/Images/FlashReplacement.jpg\\" /></td></tr>" +
            "</table></div></body><html>";
        smtpClient.Host = "DEVSMTP.corp.perfect-10.tv";
        smtpClient.Send(message);

        resultLabel.Text = "Email sent! <br />";
    }
    catch (Exception ex)
    {
        resultLabel.Text = "Couldn't Send the Message!";
    }
}
protected void clearButton_Click(object sender, EventArgs e)
{

}

}



mail.To = "email1@mycompany.com;email2@hiscompany.com;email3@hercompany.com";


Or…



mail.To.Add(new MailAddress("blah1"));
mail.To.Add(new MailAddress("blah2"));
mail.To.Add(new MailAddress("blah3"));

Hope it help.

You should loop through the address and add them to the message

foreach(MailAddress a in toAddress)
message.To.Add(a);

Cool, thanks! How do I make this work? I am getting this message from VS: foreach statement cannot operate on variables of type ‘Sytem.Net.Mail.MailAddress’ because ‘System.Net.Mail.MailAddress’ does not contain a public definition for ‘GetEnumerator’. Huh? Help please and Thanks!

Oh I see, I presume each of ur email addresses in the toTextBox is separated by “;”. Then your code should look like this.

try
{
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress(fromTextBox.Text);
string toAddressArray;
toAddressArray = TextBox1.Text.ToString().Split(new char{‘;’});
foreach (string a in toAddressArray)
{
message.To.Add(a);
}
}

Is this right? I still can’t get it to send to more than one person.

protected void sendButton_Click(object sender, EventArgs e)
{
SmtpClient smtpClient = new SmtpClient();

    try
    {
        MailAddress toAddress = new MailAddress(toTextBox.Text);

        MailMessage message = new MailMessage();
        MailAddress fromAddress = new MailAddress(fromTextBox.Text);


        message.From = fromAddress;
        message.To.Add(toAddress);
        message.Subject = subjectTextBox.Text;
        message.IsBodyHtml = true;


                    string[] toAddressArray;
        toAddressArray = toTextBox.Text.ToString().Split(new char[]{';'});
        foreach (string a in toAddressArray)
        {
            message.To.Add(a);
        }

/* End */

Here’s what the form look like if it helps… Thanks again for helping me trouble shoot this.

see comments within ur code