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?
AndyAinscowConnect With a Mentor Freelance 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());

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.
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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.

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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.