Avatar of datzent83
datzent83
Flag 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

ASP.NETProgramming.NET Programming

Avatar of undefined
Last Comment
Praveen Venu

8/22/2022 - Mon
Praveen Venu

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

PaulHews

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

datzent83

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

Thanks!
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Praveen Venu

Can you please explain what you mean?
datzent83

ASKER
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.
Praveen Venu

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

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
PaulHews

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Praveen Venu

this is what I said in a single line in my comment #26595464