Skip to main content

Design Contact Us form in Asp.Net and Setting SMTP in Web.Config

Asp Code to Design Contact Form

<div class="contact_form">
            <div class="form_row">
              <label class="contact"><strong>Name:</strong></label>
              <asp:TextBox ID="txt_name" runat ="server" placeholder="Name" BackColor="#CCCCCC" BorderStyle="Solid" ></asp:TextBox>
                 <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txt_name"
                                    CssClass="field-validation-error" ErrorMessage="The user name field is required." />
            <div class="form_row">
              <label class="contact"><strong>Email:</strong></label>
              <asp:TextBox ID="txt_email" runat ="server" placeholder="Email" BackColor="#CCCCCC" ></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txt_email"
                                    CssClass="field-validation-error" ErrorMessage="The email address field is required." />
            <div class="form_row">
              <label class="contact"><strong>Phone:</strong></label>
              <asp:TextBox ID="txt_mobile" runat ="server" placeholder="Phone" BackColor="#CCCCCC" BorderStyle="Solid" ></asp:TextBox>
               <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txt_mobile"
                                    CssClass="field-validation-error" ErrorMessage="The Phone field is required." />  
            <div class="form_row">
              <label class="contact"><strong>Message:</strong></label>
              <asp:TextBox ID="txt_msg" TextMode ="MultiLine"  runat ="server" placeholder="Message....." Height="67px" Width="225px" BackColor="#CCCCCC" ></asp:TextBox>
                  <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txt_msg"
                                    CssClass="field-validation-error" ErrorMessage="The Message field is required." />  
            <div class="form_row">  
       <asp:Button ID="btt_send" runat ="server" CssClass ="vcomp_btt" OnClick ="btt_send_Click"  Text="send" />

Code Behind Page Vb.Net.
Protected Sub btt_send_Click(sender As Object, e As EventArgs)
        Dim strmsg As String
        Dim mailTo As String = ""
        Dim mailSubject As String = "From"
        strmsg = "<table style='font-family: 'Arial Black'; color: #CC6600; border-style: inset; border-color: #008080' ><tr><td>Name :</td><td>"
        strmsg += txt_name.Text
        strmsg += "</td></tr><tr><td>Mobile Number :</td><td>"
        strmsg += txt_mobile.Text
        strmsg += "</td></tr><tr><td>Email Address :</td><td>"
        strmsg += txt_email.Text
        strmsg += "</td></tr><tr><td>Message :</td><td>"
        strmsg += txt_msg.Text
        Dim mailBody As String = strmsg
        Dim mailfrom As String = txt_email.Text
        Dim myMail As New System.Net.Mail.MailMessage(mailfrom, mailTo, mailSubject, mailBody)
        myMail.IsBodyHtml = True
            Dim c As New System.Net.Mail.SmtpClient()
            ' txtSubject.Text = ""
            'txtBody.Text = ""
        Catch myEx As System.Net.Mail.SmtpException
            System.Diagnostics.Debug.WriteLine("Error sending mail: " & myEx.Message)
            Throw myEx
        Catch ew As Exception
            myMail = Nothing
        End Try
        txt_email.Text = ""
        txt_mobile.Text = ""
        txt_msg.Text = ""
        txt_name.Text = ""
        Response.Write("<script LANGUAGE='JavaScript' >alert('Mail send Successful')</script>")
        'pm.Visible = False
        'gv.Visible = True
    End Sub

Web.Config Setting SMTP

      <smtp deliveryMethod="Network" from="admin@http://yourdomain/">
        <network host="" port="25" defaultCredentials="true"/>