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, "myemail@domain.co.uk", txtSubject.Text, sb.ToString());
           
            sc.Send(msg);
            msg.Dispose();

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

At the moment the email goes to "myemail@domain.co.uk". I presume I need to change that to the value from the RadioButton but also need to relate the value (UserA or UserB) to email@usera.com or email@userb.com.

I'm going around in circles at the moment on what should be reasonably simple.
LVL 2
nedloganAsked:
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  email@usera.com or email@userb.com.

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.
0
pschramaCommented:
string destinationEmail;

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

Or you can use a radiobuttonlist, and get the text using RadioButtonList1.SelectedItem.Text.
0
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.

Regards
0
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,  "user1@user1.co.uk", txtSubject.Text, sb.ToString());
            else if (radioButton2.Checked)
              msg = new MailMessage(txtEmail.Text,  "user2@user2.co.uk", txtSubject.Text, sb.ToString());
            else
              msg = new MailMessage(txtEmail.Text,  "myemail@domain.co.uk", txtSubject.Text, sb.ToString());




0

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
pschramaCommented:
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:

  <appSettings>
    <add key="emailUserA" value="email@usera.com"/>
  </appSettings>

Or get them from another source, depending on the requirements.
0
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.
0
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
ASP.NET

From novice to tech pro — start learning today.