Sending form email to recipient based on user choice.

Hi All,
I have a form that sends the results to my email address and works fine. However I want to send it to a different email address based on user selection.

On the form there is a list of radio buttons:

<p>My message is for:</p>
<asp:RadioButton ID="UserA" GroupName="Recipient" class="formfield"  runat="server" />User A<br />
<asp:RadioButton ID="UserB" GroupName="Recipient" class="formfield"  runat="server" />User B<br />

In the code behind I have the following:

if (IsPostBack)
            SmtpClient sc = new SmtpClient();
            StringBuilder sb = new StringBuilder();
            MailMessage msg = null;
            sb.Append("Email from: " + txtName.Text + "\n");
            sb.Append("Email: " + txtEmail.Text + "\n");
            sb.Append("Tel: " + txtPhone.Text + "\n");
            sb.Append("Question / Comments: " + txtQuestion.Text + "\n");
            msg = new MailMessage(txtEmail.Text, "", txtSubject.Text, sb.ToString());

            pnlForm.Visible = false;
            pnlConfirm.Visible = true;

At the moment the email goes to "". I presume I need to change that to the value from the RadioButton but also need to relate the value (UserA or UserB) to or

I'm going around in circles at the moment on what should be reasonably simple.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
>>need to relate the value (UserA or UserB) to or

You need to store somewhere the email adresses of the different users (unless you can hardcode them) - which also implies the user needs to enter them somewhere.

You could use the registry or a text file to store them - or if it is an application that the user can logon from different PC's then you might need a database on a central server for the storage.
string destinationEmail;

if (UserA.Checked)
   destinationEmail = UserA.Text;
else if (UserB.Checked)
   destinationEmail = UserB.Text;
   // No user checked, report error

Or you can use a radiobuttonlist, and get the text using RadioButtonList1.SelectedItem.Text.
nedloganAuthor Commented:
pschrama: I tried what you suggested but it relies on the email being defined on the form as the value of the control so if a site visitor views the source the email addresses are visible. I want it done that the email addresses are defined in the code behind file.

Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

AndyAinscowFreelance programmer / ConsultantCommented:
If you can hard code (see my first comment) then all you need is an if (or multiple if) statement

            if (radioButton1.Checked)
              msg = new MailMessage(txtEmail.Text,  "", txtSubject.Text, sb.ToString());
            else if (radioButton2.Checked)
              msg = new MailMessage(txtEmail.Text,  "", txtSubject.Text, sb.ToString());
              msg = new MailMessage(txtEmail.Text,  "", txtSubject.Text, sb.ToString());


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Then instead of setting destinationEmail to UserA.Text or UserB.Text, you can directly set it to a specific address in the code.
It might be better to use an Application Setting though, like this:

if (UserA.Checked)
   destinationEmail = ConfigurationManager.AppSettings["emailUserA"];

For this you need to add the setting to the appsettings in web.config:

    <add key="emailUserA" value=""/>

Or get them from another source, depending on the requirements.
nedloganAuthor Commented:
Thanks, I was creating the if / else further up and passing values down (if that makes sense).

Anyway, clear and simple thanks for your help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.