Send email to one or more addresses

Last post 05-14-2008 9:12 PM by musicman21. 2 replies.

Sort Posts:

  • Send email to one or more addresses

    05-14-2008, 5:07 PM
    • Loading...
    • musicman21
    • Joined on 02-05-2008, 7:47 PM
    • Posts 31

     I have a page that contains several checkboxes that control who an email is sent to. I have no problem making it email when I select all the the boxes, but I need to have the option to send to just one address if need be. When I don't select all the checkboxes, meaning the message doesn't goes out to everyone, it fails. Only when they are all selected does it work. Any help would be appreciated.

     protected void Button1_Click(object sender, EventArgs e)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("TB1: " + TextBox3.Text + "<br />");
                sb.Append("TB2: " + TextBox4.Text + "<br />");
               
               
                MailMessage mail = new MailMessage();

                mail.From = new MailAddress("webmaster@mscc.edu");
                mail.To.Add(TextBox1.Text);
                mail.Bcc.Add(TextBox2.Text);

                mail.Subject = "This is a test";
                mail.Body = sb.ToString();
                mail.IsBodyHtml = true;
           
                SmtpClient smtp = new SmtpClient("server");
              
                smtp.Credentials = new System.Net.NetworkCredential("user", "password");
                smtp.Send(mail);
            }

     

    When a checkbox is selected it populates a hidden textbox with the associated email address for that person. If there's a better way to do that aspect I'd like to hear it.

  • Re: Send email to one or more addresses

    05-14-2008, 5:34 PM
    Answer
    • Loading...
    • johram
    • Joined on 06-13-2006, 6:36 AM
    • Sweden
    • Posts 2,802
    • Moderator

    Do you really need to store the email addresses in hidden textboxes? Do they have to be on the client at all? The logic could do this on server side without having to make a round via the client. Theory:

    List<string> recipients = new List<string>();
    if (checkbox1.Checked)
       recipients.Add("user1@somedomain.com");
    if (checkbox2.Checked)
       recipients.Add("user2@somedomain.com");
    if (checkbox3.Checked)
       recipients.Add("user3@somedomain.com");
    

    Of course this information could be kept in a database and the checks could be performed in a loop, but my example was just to give you the idea.

    Is there a pattern for who gets to be in the To: field and who gets the Bcc: field? Otherwise, it is very easy to dynamically populate recipients regarding on which checkboxes are checked. Given the previous code, you could add the recipients with one line of code:

    mail.To.Add(String.Join(",", recipients.ToArray());
      
    If this post was useful to you, please mark it as answer. Thank you!
  • Re: Send email to one or more addresses

    05-14-2008, 9:12 PM
    • Loading...
    • musicman21
    • Joined on 02-05-2008, 7:47 PM
    • Posts 31

    I like what you've done, it makes perfect sense. The To: field will be hard-coded because the email will always go to that person. The number of Bcc: fields will be minimal, 4-5. You mention storing the addresses in a database and retrieving them. I know how to retrieve but based on my knowledge I'd have to store it in a hidden textbox. Could you give me an example of how I would do it your way? I'd like to get away from doing it my way and do it the correct way.
     

Page 1 of 1 (3 items)