title: Sending an email once a form is submitted using smtp and gmail in asp.net
body: <p>I'm trying to receive an email once my form is submitted on my webpage. At the moment it submits fine without any errors but I don't receive the email. Does anyone know what code I have to add in the code behind page to make this work?</p>
  
  <p>Here is the html;</p>
  
  <pre><code>&lt;h2&gt;Contact Us&lt;/h2&gt;
          &lt;br /&gt;
          &lt;table&gt;            
              &lt;tr&gt;
                  &lt;td  style="align-items:center"&gt;
                      Name:&lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:TextBox ID="txtName"
                                      runat="server"
                                      Columns="40"&gt;&lt;/asp:TextBox&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td  style="align-items:center"&gt;
                      email:&lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:TextBox ID="txtEmail"
                                      runat="server"
                                      Columns="40"&gt;&lt;/asp:TextBox&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
  
  
  
              &lt;!-- Message --&gt;
              &lt;tr&gt;
                  &lt;td style="align-items:center"&gt;
                      What are you looking for?
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:TextBox ID="txtMessage"
                                      runat="server"
                                      Columns="40"
                                      Rows="6"
                                      TextMode="MultiLine"&gt;&lt;/asp:TextBox&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
               &lt;tr&gt;
                  &lt;td  style="align-items:center"&gt;
                      What would you be willing to pay for this app?&lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:TextBox ID="txtPay"
                                      runat="server"
                                      Columns="40"&gt;&lt;/asp:TextBox&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
  
              &lt;!-- Submit --&gt;
              &lt;tr style="align-items:center"&gt;
                  &lt;td colspan="2"&gt;
                      &lt;asp:Button ID="btnSubmit" runat="server" Text="Submit"
                          onclick="btnSubmit_Click" /&gt;&lt;br /&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
  
              &lt;!-- Results --&gt;
              &lt;tr style="align-items:center"&gt;
                  &lt;td colspan="2"&gt;
                      &lt;asp:Label ID="lblResult" runat="server"&gt;&lt;/asp:Label&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
          &lt;/table&gt;
  </code></pre>
  
  <p>this is the code behind;</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Net;
  using System.Net.Mail;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  
  public partial class Telluswhatyouwant : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
  
      }
      protected void btnSubmit_Click(object sender, EventArgs e)
      {
  
          try
          {
              //Create the msg object to be sent
              MailMessage msg = new MailMessage();
              //Add your email address to the recipients
              msg.To.Add("ronan.byrne@mhlabs.net");
  
  //Send the msg
              client.Send(msg);
  </code></pre>