Send form content by email in ASP.NET

Using ASP.NET to send the contents of a form by email is a common requirement. This sample looks at generating an email using System.Net.Mail in both plain text and html format from a very basic Contact Us form.

First, the form (without the button).

Please provide your comments

Your name:

Your email address:

Your comments:

And the relevant .aspx markup (with the button).

<p>Please provide your comments</p>
<p>Your name:<br />
<asp:TextBox ID="YourName" runat="server" Width="150px" /><br />
Your email address:<br />
<asp:TextBox ID="YourEmail" runat="server" Width="150px" /><br />
Your comments:<br />
<asp:TextBox ID="Comments" runat="server" TextMode="MultiLine" Rows="10" Width="400px" />
</p>
<p>
<asp:Button ID="Button1" runat="server" Text="Send" OnClick="Button1_Click" />
</p> 

There are 3 server controls: YourName, YourEmail and Comments. It is the values from these controls that will be supplied to the email to provide a From address and body content for the email. The code that runs as a result of Button1_Click is straightforward.

Plain Text:

[C#]

using (MailMessage message = new MailMessage())
{  
  message.From = new MailAddress(YourEmail.Text.ToString());  
  message.To.Add(new MailAddress("[email protected]"));  
  message.CC.Add(new MailAddress("[email protected]"));  
  message.Subject = "Message via My Site from " + YourName.Text.ToString();  
  message.Body = Comments.Text.ToString();  
  SmtpClient client = new SmtpClient();  
  client.Host = "127.0.0.1";  
  client.Send(message);    
}
[VB]

Using message As New MailMessage()
  message.From = New MailAddress(YourEmail.Text.ToString())
  message.[To].Add(New MailAddress("[email protected]"))
  message.CC.Add(New MailAddress("[email protected]"))
  message.Subject = "Message via My Site from " + YourName.Text.ToString()
  message.Body = Comments.Text.ToString()
  Dim client As New SmtpClient()
  client.Host = "127.0.0.1"
  client.Send(message)
End Using

HTML

[C#]
using (MailMessage message = new MailMessage())
{  
  message.From = new MailAddress(YourEmail.Text.ToString());  
  message.To.Add(new MailAddress("[email protected]"));  
  message.CC.Add(new MailAddress("[email protected]"));  
  message.Subject = "Message via My Site from " + YourName.Text.ToString();  
  message.IsBodyHtml = true;  
  message.Body = "<html><body>" + Comments.Text.ToString() + "</body></html>;  
  SmtpClient client = new SmtpClient();  
  client.Host = "127.0.0.1";  
  client.Send(message);    
}
[VB]

Using message As New MailMessage()
  message.From = New MailAddress(YourEmail.Text.ToString())
  message.[To].Add(New MailAddress("[email protected]"))
  message.CC.Add(New MailAddress("[email protected]"))
  message.Subject = "Message via My Site from" + YourName.Text.ToString()
  message.IsBodyHtml = True
  message.Body = "<html><body>" & Comments.Text.ToString() & "</body></html>"
  Dim client As New SmtpClient()
  client.Host = "127.0.0.1"
  client.Send(message)
End Using