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("me@domain.com"));  
  message.CC.Add(new MailAddress("copy@domain.com"));  
  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("me@domain.com"))
  message.CC.Add(New MailAddress("copy@domain.com"))
  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("me@domain.com"));  
  message.CC.Add(new MailAddress("copy@domain.com"));  
  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("me@domain.com"))
  message.CC.Add(New MailAddress("copy@domain.com"))
  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

Date Posted:
Last Updated:
Posted by:
Total Views to date: 325354

33 Comments

- Mrityunjay Kishor

Dear Sir,
I want to get information filled by user in form on my website sent to my email. you have provided good coding here. thanks for that. but i want to know i m using id of user as FROMID, and my id as TOID. will it be possible to send the content of form to any email without providing username and password of FROM ID.
If yes, then will the mail get stored in sent folder of FROM ID.

- Mike

@Mrityunjay

I'm not at all clear what you are asking, but I recommend that if you have a question about how to do something in ASP.NET, you as at the forums at www.asp.net. You will get a much quicker answer there.

- patric

ur code is not at all work

- Mike

@Patric

My code works fine. I suspect you mean that *your* code doesn't work at all.

- Jesse Quintanilla

This is a test

- Mike

@Jesse

Thanks ever so much for testing my comments form. As you can see, it works.

Some people are just weird.

- Prasad

hiiiiiii

i wnt to send an email from my localmachine ,
and this my code
Using message As New MailMessage()
message.From = New MailAddress(YourEmail.Text.ToString())
message.[To].Add(New MailAddress("mike@mikesdotnetting.com"))
message.CC.Add(New MailAddress("copy@mikesdotnetting.com"))
message.Subject = "Message via Mikesdotnetting from " + YourName.Text.ToString()
message.Body = Comments.Text.ToString()
Dim client As New SmtpClient()
client.Host = "198.162.2.100:80"
'client.Send(message)
End Using


so , can u please help me to send an email fro local machine

- Ahmad Tahir

Hi

client.Host = "127.0.0.1";

Please tell me what do this line do?

do i have to change it to something else to make it work......?

Please reply,

Thank You very much,\\Best Regards,

Ahmad Tahir.

- Ahmad Tahir

one more problem

i am having a problem

Type 'MailMessage' is not defined?

Please Help,

Thanks.

- Mike

@Ahmad

127.0.0.1 is a local loopback address (can be known as localhost) and points to the IP of the local SMTP server.

As far as your MailMessage is not defined problem is concerned, you need to make sure you add "using System.Net.Mail" to the top of your code-behind (C#), or "Imports System.Net.Mail" if you are using VB.

- Ahmad Tahir

Thank You very much Sir,

This is really so nice of you, this article helped me alot, i am desperately looking for a solution to send me a copy of the body of the post the user types on an comment box in my site but i am totally stuck.

Please tell me what i have to to do if i want to e-mail at a gmail address.

Here is a screenshot of the error i am getting,

Thank You very Much.

- Mike

@Ahmad,

There are a number of configurations you need to use Gmail. Here's an item that shows them: http://geekswithblogs.net/aymanfm/archive/2006/03/09/71868.aspx

If you continue to have problems, post a question to the forums at http://forums.asp.net

- Tracey Rickard

I am sorry if I am going to sound a bit stupid, I am not a programmer. I have been using MS Expression Web 2. I have created an ASP Form with text boxes, submit button and validation. I have been able to create a PostBackUrl and that works OK, so the form is OK. I can see how I can edit your form VB code to match my text fields but what I can't work out is where to phsyically put the code that runs as a result of the button press. Please can you help? Tracey.

- Randall


I am having the same issue... forgive me for being a noob...
"
20 April 2009 18:37 from Ahmad Tahir

one more problem

i am having a problem

Type 'MailMessage' is not defined?
"


- Mike

@Randall,

Both of the questions you ask have already been answered in other comments.

- theertha

Hi,

content of multiple textbox is not coming correctly.

if i enter,

one
two
three

then in the mail content

one two three is coming...

cant we multiline text box content show in the same format as it is entred.

thanks

- Mike

@theertha

You need to use

String.Replace(Environment.NewLine, "<br />")

- Ryan

Is there anyway you can show us how to write the anti-spam (2+7) question that you use on this site?

- Mike

- Emad

Thanks for the excellent snippet of code. I am trying to send a much more complicated html form (a user registration form with address fields, etc.) to an email address. How can I accomplish this since the body of the email won't be a simple textbox value? You're help would be greatly appreciated!

- sagheer

Where should i put my Email to receive these comments ?

- Mike

@Emad,

Have a look at http://www.systemnetmail.com for more detailed coverage of sending html emails.

- Mike

@sagheer,

You would replace my email address in the code snippet with yours.

- alfredo girosi

how do I include radio buttons feedback in the email?

- Mike

@alfredo

Just check the Checked property to see which one has been selected. Then construct a string accordingly.

- beckham1

These kind of post are always inspiring and I prefer to read quality content so I happy to find many good point here in the post, writing is simply great, thank you for the post.

- Andolasoft

Very helpful post! You made it so easy.

- Galen

Hello MikesDotnetting,
Just wanted to say the mail Coding you posted here works perfect!!! Keep up the Good work:)

- tom

Can you upload your verison of the working code? There seems to be a problem with mine and the local host ip.
Thanks for your time.

- sara

good post

- Anathi Soga

Thank you

- Shubham

Thank you..
it helped me alot...

- Nevil Patel

Superb stuff and article.I just loved reading it. Keep writing.

Recent Comments

Mike 6/29/2015 2:22 AM
In response to MVC 5 with EF 6 in Visual Basic - Sorting, Filtering and Paging
This is the first example that I have found that works....

Marty 6/28/2015 4:57 AM
In response to Posting Data With jQuery AJAX In ASP.NET Razor Web Pages
Mike, what if I don't want to render back the text to the browser, but I want to send it some other...

Mike 6/27/2015 4:00 PM
In response to Migrating Classic ASP To ASP.NET Razor Web Pages Part One- Razor Syntax And Visual Basic
have you used any of the code converters to convert classic asp to c#? If so, which one do you have...

deepan 6/27/2015 12:49 PM
In response to How To Send Email In ASP.NET MVC
Your article is very very useful for me...thanks for giving this article ...this is very simple to u...

Howie 6/27/2015 6:20 AM
In response to Scheduled Tasks In ASP.NET With Quartz.Net
Mike - Great article. I'm curious about the lifetime of the scheduler - I see your is static and you...

Chris 6/26/2015 7:42 PM
In response to Ajax with Classic ASP using jQuery
I have searched the web high and low for a clean simple solution using JQuery, JSON, and Classic ASP...

aaron 6/26/2015 12:01 PM
In response to 7 C# 6.0 Features That Every ASP.NET Developer Should Know About
#6 enables a less-verbose functional programming style...

Marty 6/26/2015 5:26 AM
In response to Posting Data With jQuery AJAX In ASP.NET Razor Web Pages
Great article. You don't show the code in Receiver.cshtml to send data back. How would you send text...

Vimal 6/25/2015 11:09 AM
In response to iTextSharp - Working with Fonts
I want to edit my existing PDF. I want to change font color. I want to change is gray or some I to...

yoyo 6/24/2015 9:00 PM
In response to Dependency Injection and Inversion of Control with ASP.NET MVC
Hi, thanks for your explainations. Lionel...