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: Thursday, May 10, 2007 10:53 PM
Last Updated: Friday, January 14, 2011 6:31 PM
Posted by: Mikesdotnetting
Total Views to date: 270354

33 Comments

Thursday, February 26, 2009 6:39 AM - 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.

Thursday, February 26, 2009 6:57 PM - 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.

Friday, March 13, 2009 5:12 AM - patric

ur code is not at all work

Friday, March 13, 2009 7:11 AM - Mike

@Patric

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

Tuesday, March 24, 2009 3:55 AM - Jesse Quintanilla

This is a test

Tuesday, March 24, 2009 7:51 AM - Mike

@Jesse

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

Some people are just weird.

Friday, April 17, 2009 6:57 AM - 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

Monday, April 20, 2009 6:27 PM - 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.

Monday, April 20, 2009 6:37 PM - Ahmad Tahir

one more problem

i am having a problem

Type 'MailMessage' is not defined?

Please Help,

Thanks.

Monday, April 20, 2009 7:07 PM - 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.

Monday, April 20, 2009 8:30 PM - 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.

Monday, April 20, 2009 8:53 PM - 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

Tuesday, April 28, 2009 9:01 AM - 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.

Thursday, April 30, 2009 3:28 PM - 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?
"


Monday, May 4, 2009 7:41 PM - Mike

@Randall,

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

Friday, July 24, 2009 1:17 PM - 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

Monday, July 27, 2009 2:09 PM - Mike

@theertha

You need to use

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

Thursday, November 26, 2009 5:39 AM - Ryan

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

Thursday, November 26, 2009 7:34 PM - Mike

Thursday, December 17, 2009 4:33 AM - 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!

Wednesday, December 30, 2009 7:08 AM - sagheer

Where should i put my Email to receive these comments ?

Wednesday, December 30, 2009 8:01 AM - Mike

@Emad,

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

Wednesday, December 30, 2009 8:02 AM - Mike

@sagheer,

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

Wednesday, February 10, 2010 4:41 AM - alfredo girosi

how do I include radio buttons feedback in the email?

Friday, February 12, 2010 8:49 PM - Mike

@alfredo

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

Friday, April 16, 2010 7:50 AM - 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.

Thursday, March 31, 2011 11:40 AM - Andolasoft

Very helpful post! You made it so easy.

Monday, December 10, 2012 3:37 PM - Galen

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

Tuesday, January 22, 2013 9:41 PM - 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.

Friday, March 15, 2013 4:06 PM - sara

good post

Friday, July 26, 2013 7:52 AM - Anathi Soga

Thank you

Friday, May 16, 2014 5:18 AM - Shubham

Thank you..
it helped me alot...

Wednesday, August 27, 2014 10:19 AM - Nevil Patel

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

If you have any comments to make about this article, please use this form to do so. Make sure that your comment relates specifically to the article above. More general comments can be posted through the form on the Contact page.

Please note, all comments are moderated, and some may not be published. The kind of things that will ensure your comment is deleted without ever seeing the light of day are as follows:

  • Not relevant to the article
  • Gratuitous links to your own site or product
  • Anything abusive or libellous
  • Spam
  • Anything in a language I don't understand including gibberish.

I do not pass email addresses on to spammers, so a valid one will assist me in responding to you personally if required.

Recent Comments

Gautam 11/20/2014 8:01 AM
In response to I'm Writing A Book On WebMatrix
Hello Mike, I read your book, loved it! However, I have a few request/suggestions: 1) an example...

Bret Dev 11/19/2014 8:39 PM
In response to The Difference Between @Helpers and @Functions In WebMatrix
Excellent post! One concern - where can you place global @Functions code within an MVC project to Is...

Rob Farquharson 11/19/2014 4:28 PM
In response to iTextSharp - Links and Bookmarks
How can I place text at an absolute position on the page? Also, how can I rotate text?...

Andy 11/17/2014 8:08 PM
In response to MVC 5 with EF 6 in Visual Basic - Sorting, Filtering and Paging
Hello I'm testing your sorting instructions above. This is great and I was able to get it to work...

Gautam 11/17/2014 5:51 PM
In response to WebMatrix - Database Helpers for IN Clauses
Hi Mike, I am very new to programming: In the above example if I want to use a delete button the...

donramon 11/17/2014 3:22 PM
In response to Entity Framework 6 Recipe - Alphabetical Paging In ASP.NET MVC
Congratulations on your new website look and the excellent articles. Thank you!...

Gautam 11/17/2014 11:26 AM
In response to Looking At The WebMatrix WebGrid
Hi Mike, I add the jquery script at the end of my html file.. when ajax attribute is added to the be...

Chet Ripley 11/15/2014 6:57 PM
In response to Adding A New Field
It appears the command is case sensitive. I had the same issue as Cameron. When I changed the to it...

Alvin 11/14/2014 12:49 PM
In response to Razor Web Pages E-Commerce - Adding A Shopping Cart To The Bakery Template Site
Great article Mike! When do you plan to extend the bakery shopping cart beyond this point?...

Gautam 11/14/2014 10:16 AM
In response to Web Pages - Efficient Paging Without The WebGrid
to get the count can we use only the below sql, why to join category and author table var sql =...