Send form content by email in ASP.NET
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
Currently rated 3.70 by 30 people
Rate Now!
Date Posted:
10 May 2007 22:53
Last Updated:
14 January 2011 18:31
Posted by:
Mikesdotnetting
Total Views to date:
119900



Comments
26 February 2009 06:39 from 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.
26 February 2009 18:57 from Mikesdotnetting
@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.
13 March 2009 05:12 from patric
ur code is not at all work
13 March 2009 07:11 from Mikesdotnetting
@Patric
My code works fine. I suspect you mean that *your* code doesn't work at all.
24 March 2009 03:55 from Jesse Quintanilla
This is a test
24 March 2009 07:51 from Mikesdotnetting
@Jesse
Thanks ever so much for testing my comments form. As you can see, it works.
Some people are just weird.
17 April 2009 06:57 from 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
20 April 2009 18:27 from 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.
20 April 2009 18:37 from Ahmad Tahir
one more problem
i am having a problem
Type 'MailMessage' is not defined?
Please Help,
Thanks.
20 April 2009 19:07 from Mikesdotnetting
@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.
20 April 2009 20:30 from 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.
20 April 2009 20:53 from Mikesdotnetting
@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
28 April 2009 09:01 from 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.
30 April 2009 15:28 from 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?
"
04 May 2009 19:41 from Mikesdotnetting
@Randall,
Both of the questions you ask have already been answered in other comments.
24 July 2009 13:17 from 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
27 July 2009 14:09 from Mikesdotnetting
@theertha
You need to use
String.Replace(Environment.NewLine, "<br />")
26 November 2009 05:39 from Ryan
Is there anyway you can show us how to write the anti-spam (2+7) question that you use on this site?
26 November 2009 19:34 from Mikesdotnetting
@Ryan
It's detailed in a more recent article here:
http://www.mikesdotnetting.com/Article/106/A-Degradable-jQuery-AJAX-Email-Form-for-ASP.NET-MVC
17 December 2009 04:33 from 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!
30 December 2009 07:08 from sagheer
Where should i put my Email to receive these comments ?
30 December 2009 08:01 from Mikesdotnetting
@Emad,
Have a look at http://www.systemnetmail.com for more detailed coverage of sending html emails.
30 December 2009 08:02 from Mikesdotnetting
@sagheer,
You would replace my email address in the code snippet with yours.
10 February 2010 04:41 from alfredo girosi
how do I include radio buttons feedback in the email?
12 February 2010 20:49 from Mikesdotnetting
@alfredo
Just check the Checked property to see which one has been selected. Then construct a string accordingly.
16 April 2010 07:50 from 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.
31 March 2011 11:40 from Andolasoft
Very helpful post! You made it so easy.
10 December 2012 15:37 from Galen
Hello MikesDotnetting,
Just wanted to say the mail Coding you posted here works perfect!!! Keep up the Good work:)
22 January 2013 21:41 from 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.
15 March 2013 16:06 from sara
good post