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

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.
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 I end up deleting quite a lot. The kind of things that will ensure your comment is deleted without ever seeing the light of day are as follows:

  • Requests to fix your code (post a question to forums.asp.net instead, please)
  • Gratuitous links to your own site or product
  • Anything abusive or libellous
  • Spam

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

Lorenzo 3/26/2015 8:21 AM
In response to iTextSharp - Introducing Tables
Hi Mike How can I add padding to all cells in the table? Kind Regards Lorenzo...

Satyabrata Mohapatra 3/25/2015 8:11 AM
In response to How To Send Email In ASP.NET MVC
Great article. Simple and up to the point....

Afzaal Ahmad Zeeshan 3/24/2015 8:17 PM
In response to How To Send Email In ASP.NET MVC
A great way to teach the MVC framework for sending the emails... Also, what I found helpful was the...

Jim H 3/24/2015 2:32 PM
In response to Migrating From Razor Web Pages To ASP.NET MVC 5 - Model Binding And Forms
Thank you. This helps....

wazz 3/22/2015 5:48 AM
In response to Posting Data With jQuery AJAX In ASP.NET Razor Web Pages
great info!!...

rael 3/21/2015 8:53 PM
In response to Getting the identity of the most recently added record
I spent hours trying to figure how to achieve this in C#. This article helped me. Thanks a lot...

Stephen 3/21/2015 8:48 PM
In response to Ajax with Classic ASP using jQuery
This was very helpful, thanks:)...

patrick voes 3/19/2015 10:19 AM
In response to iTextSharp - Introducing Tables
Thank you! very helpfull....

Bigmachini 3/19/2015 6:13 AM
In response to ASP.NET MVC DropDownLists - Multiple Selection and Enum Support
This just made my day, afternoon, evening, night... was looking for a way of doing this without to a...

Bobbyg 3/19/2015 4:14 AM
In response to HTML Helpers For Forms In Razor Web Pages
Nice article. There are bugs in MVC 5 with dropdowns retaining values from other screens and them. I...