Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Sending Emails Depending on Dropdown List Selection

Options
  • 06-04-2009 11:03pm
    #1
    Registered Users Posts: 325 ✭✭


    Hi

    I'm using C# and Visual Studio 2008

    I have a form that people fill out, that has a Dropdown List at the top.

    When they click send I want it to send to a specific email address depending on the selection they made in the Dropdown List.

    Here is the code I have and it works but just sends it to the one email address "support@sample.com":

    using (MailMessage message = new MailMessage())
    {
    message.From = new MailAddress(txtCompanyName.Text.ToString());
    message.To.Add(new MailAddress("support@sample.com"));
    message.Subject = txtCompanyName.Text.ToString();
    message.Body = txtInformation.Text.ToString();
    SmtpClient client = new SmtpClient();
    client.Host = "cp";
    client.Send(message);

    }

    Could someone tell me how I could do this?

    Cheers


Comments

  • Registered Users Posts: 15,065 ✭✭✭✭Malice


    This is just off the top of my head but it should work:

    [PHP]using (MailMessage message = new MailMessage())
    {
    message.From = new MailAddress(txtCompanyName.Text.ToString());
    string destinationAddress = lstAddress.SelectedValue;
    message.To.Add(new MailAddress(destinationAddress));
    message.Subject = txtCompanyName.Text.ToString();
    message.Body = txtInformation.Text.ToString();
    SmtpClient client = new SmtpClient();
    client.Host = "cp";
    client.Send(message);

    }[/PHP]
    Replace lstAddress with the name of the control with the addresses in it. This also assumes that the listbox contains the addresses as the value attribute i.e. the listbox displays names like "John Smith" or whatever but the value would be "johnsmith@somewhere.com".


  • Registered Users Posts: 325 ✭✭doyler442


    malice_ wrote: »
    string destinationAddress = lstAddress.SelectedValue;
    message.To.Add(new MailAddress(destinationAddress));

    This worked perfectly - thanks very much.


  • Registered Users Posts: 7,518 ✭✭✭matrim


    malice_ wrote: »
    This is just off the top of my head but it should work:

    [PHP]using (MailMessage message = new MailMessage())
    {
    message.From = new MailAddress(txtCompanyName.Text.ToString());
    string destinationAddress = lstAddress.SelectedValue;
    message.To.Add(new MailAddress(destinationAddress));
    message.Subject = txtCompanyName.Text.ToString();
    message.Body = txtInformation.Text.ToString();
    SmtpClient client = new SmtpClient();
    client.Host = "cp";
    client.Send(message);

    }[/PHP]
    Replace lstAddress with the name of the control with the addresses in it. This also assumes that the listbox contains the addresses as the value attribute i.e. the listbox displays names like "John Smith" or whatever but the value would be "johnsmith@somewhere.com".

    Just one point on this, I wouldn't have the address as the list box value. It leaves them open to being read by spam bots, and also someone could submit a fake address and you will send to it unless you do some checking to make sure it's one of your addresses.

    If you know the addresses you want to send to, have the input being something else and pick based on that

    e.g. input value is a number so use a switch to pick the correct address
    switch (lstAddress.SelectedValue)
    {
       case 1:
          destinationAddress = "someone@address.com"
          break;
       case 2:
          destinationAddress = "someoneelse@address.com"
          break;
       default:
          Invalid entry return error to user
    }
    


  • Registered Users Posts: 15,065 ✭✭✭✭Malice


    That's a good couple of points there matrim!

    One other thing I would do is add a try/catch block to the e-mail sending function call because it can quite easily fail for a variety of reasons and you will want to know about it when it happens.


Advertisement