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: 329298

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

sandeep 8/28/2015 7:03 AM
In response to 7 C# 6.0 Features That Every ASP.NET Developer Should Know About
very good article, i like it........keep writing such quality article in future. thx Mike....

Hassan, MVC Learner 8/28/2015 6:37 AM
In response to Get The Drop On ASP.NET MVC DropDownLists
Great Help, simple, great and patiently explained article !...

Anvesh 8/28/2015 12:39 AM
In response to ASP.NET MVC DropDownLists - Multiple Selection and Enum Support
what if we are taking postback values from FormCollections instead of an array int[] category. How...

Ben 8/27/2015 10:50 PM
In response to Simple File Download Protection with ASP.NET
Is it possible to setup your project to publish files to outside of your root directory? I would to...

Fred 8/26/2015 12:50 AM
In response to WebMatrix Opens Wrong Version Of Visual Studio
I enjoyed many of your tutorials but the problem is none of the tutorials are combined like most be....

Muhammad Ashikuzzaman 8/25/2015 2:48 PM
In response to Managing Checkboxes And Radios In ASP.NET Razor Web Pages
That's a very good tips for razor...

Sergey 8/25/2015 8:32 AM
In response to More Flexible Routing For ASP.NET Web Pages
Hi. How I can set up my site to get urldata from link for default page? site.com/default/1-...

Tony Gray 8/25/2015 6:27 AM
In response to Adding Validation
Hi Mike, Really helpful article and series. Thanks. Small typo in 4th paragraph you have so...

amanda n 8/25/2015 12:38 AM
In response to Solving the Operation Must Use An Updateable Query error
Thank you very much. I'm a uni student and while solving coding problems is usually enjoyable and me...

salman 8/23/2015 9:25 AM
In response to How To Send Email In ASP.NET MVC
thanks ......