title: Send email in ASP.NET when database record is inserted
body: <p>I have a ASP.NET FormView which allows for inserting of a record into a SQL Server table. How can I have an email be sent from the application once the record is successfully added to the table after the "Insert" button is clicked in an <code>&lt;InsertItemTemplate&gt;</code>?</p>
  
  <p>I would like to be able to specify the to, from, subject, body, etc. of the email.</p>
  
  <p>I'm using .NET 4.0 and C#.</p>
  
  <p><strong>.aspx page:</strong></p>
  
  <pre><code>&lt;asp:FormView ID="formViewNewOrder" runat="server" DataKeyNames="Order_ID" DataSourceID="dsource1"&gt;
      &lt;InsertItemTemplate&gt;
      &lt;b&gt;Order Number:&lt;/b&gt; &lt;asp:TextBox ID="txtInsertOrderNo" runat="server" Text='&lt;%# Bind("Order_Number") %&gt;' /&gt;
      &lt;br /&gt;
      &lt;br /&gt;
      &lt;asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert" onclick="InsertButton_Click" /&gt;
              &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 
      &lt;asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"    Text="Cancel" OnClick="InsertCancelButton_Click" /&gt;
      &lt;/InsertItemTemplate&gt;
  &lt;/asp:FormView&gt;
  </code></pre>
  
  <p><strong>Code-behind:</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Data;
  //Namespaces to be added?
  
  protected void InsertButton_Click(object sender, EventArgs e)
  {
  
      //Email code here ?
  
  }
  </code></pre>