Link to home
Start Free TrialLog in
Avatar of datzent83
datzent83Flag for United States of America

asked on

VB.NET Email with Checkboxes

In the following code, how do I add

If checkbox1 is checked then add "checkbox 1 is checked"
If checkbox2 is checked then add "checkbox 2 is checked"

 into the body of the email?
<%@ Page Language="VB" Debug="true" %>
<% @Import Namespace="System.Web.Mail" %>
<script language="vb" runat="server">

Sub Send2Mail (sender as Object, e as EventArgs)

Dim objMail as New MailMessage()

  objMail.To = "test@test.com"
  objMail.From = strEmail.Text

  objMail.BodyFormat = MailFormat.Text
  objMail.Priority = MailPriority.Normal
  objMail.Subject = "Web Contact"

  objMail.Body = "Name: " + strName.Text + vbNewLine + "Email: " + strEmail.text + vbnewLine + "Work Phone: " + strWrkPhone.Text + vbnewLine + "Home Phone: " + strHomePhone.Text + vbnewLine + "Fax Number: " + strFaxNumber.Text + vbnewLine + "Message: " + strYourMsg.text
   
  SmtpMail.SmtpServer = "localhost"
  SmtpMail.Send(objMail)
   
  Response.Redirect("email_confirm.htm")

End Sub

</script>

Open in new window

Avatar of Praveen Venu
Praveen Venu
Flag of India image

Change line16 to
objMail.Body = "Name: " + strName.Text + vbNewLine + "Email: " + strEmail.text + vbnewLine + "Work Phone: " + strWrkPhone.Text + vbnewLine + "Home Phone: " + strHomePhone.Text + vbnewLine + "Fax Number: " + strFaxNumber.Text + vbnewLine + "Message: " + strYourMsg.text + vbNewLine + IIF(checkbox1.checked,"Checkbox1 is checked","") + vbNewLine + IIF(checkbox2.checked,"Checkbox2 is checked","")

Open in new window

You can append to the body property conditionally.
Sub Send2Mail(ByVal sender As Object, ByVal e As EventArgs)

        Dim objMail As New MailMessage()

        objMail.To = "igors@optimumcomp.com"
        objMail.From = strEmail.Text

        objMail.BodyFormat = MailFormat.Text
        objMail.Priority = MailPriority.Normal
        objMail.Subject = "HealthyChats.com Spanish Web Contact"

        objMail.Body = "Name: " + strName.Text + vbNewLine + "Email: " + strEmail.text + vbnewLine + "Work Phone: " + strWrkPhone.Text + vbnewLine + "Home Phone: " + strHomePhone.Text + vbnewLine + "Fax Number: " + strFaxNumber.Text + vbnewLine + "Message: " + strYourMsg.text
        If checkbox1.Checked Then
            objMail.Body &= vbNewLine & "checkbox1 is checked"
        End If
        If checkbox2.Checked Then
            objMail.Body &= vbNewLine & "checkbox2 is checked"
        End If

        SmtpMail.SmtpServer = "localhost"
        SmtpMail.Send(objMail)

        Response.Redirect("email_confirm.htm")

    End Sub

Open in new window

Avatar of datzent83

ASKER

PaulHews: Is there a way to break each text onto a new line and move it above the name?

Thanks!
Can you please explain what you mean?
When the email is recieved all the checkboxs texts is below the message (strYourMsg). I need it to be above the name (strName).
I need the email address and the phone numbers to be validated as well to make sure they type in the proper email addresses and phone numbers.
change line 16 to
objMail.Body =IIF(checkbox1.checked,"Checkbox1 is checked","") + vbNewLine + IIF(checkbox2.checked,"Checkbox2 is checked","")+ vbNewLine + "Name: " + strName.Text + vbNewLine + "Email: " + strEmail.text + vbnewLine + "Work Phone: " + strWrkPhone.Text + vbnewLine + "Home Phone: " + strHomePhone.Text + vbnewLine + "Fax Number: " + strFaxNumber.Text + vbnewLine + "Message: " + strYourMsg.text

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of PaulHews
PaulHews
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
this is what I said in a single line in my comment #26595464