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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Sending Emails Depending on Dropdown List Selection

  • 06-04-2009 10:03pm
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 Posts: 15,070 ✭✭✭✭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, Registered Users 2 Posts: 325 ✭✭doyler442


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

    This worked perfectly - thanks very much.


  • Registered Users, Registered Users 2 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, Registered Users 2 Posts: 15,070 ✭✭✭✭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