Sending email from a web page is one of the most common functionality required from a web site.
The System.Web.Mail Namespace in the .NET Framework contains the required support classes and enumerations for email capabilities.
The main classes used are System.Web.Mail.MailMessage and the SmtpMail class.
Listing1 contains the listing for a simple web form to send email. This form assumes that the SMTP service is running on the server. private void btnSend_Click(object sender, System.EventArgs e) { MailMessage msg = new MailMessage(); msg.To = txtTo.Text; msg.From = txtFrom.Text; msg.Subject = txtSubject.Text; msg.Body = txtContent.Value; SmtpMail.Send(msg); lblStatus.Text = "Sent email (" + txtSubject.Text + ") to " +txtTo.Text; } </script> <html> <body> <h3> Email From ASP.NET</h3> <form id="MailForm" method="post" runat="server"> <asp:Label ID="Label1" Style="left: 100px; position: absolute; top: 100px" runat="server">From:
</asp:Label> <asp:TextBox ID="txtFrom" Style="left: 200px; position: absolute; top: 100px"
runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="FromValidator1" Style="left: 100px; position: absolute; top: 375px" runat="server" ErrorMessage="Please Enter the Email From." Width="200px" Height="23px" ControlToValidate="txtFrom"></asp:RequiredFieldValidator> <asp:RegularExpressionValidator ID="FromValidator2" Style="left: 100px; position: absolute; top: 400px" runat="server" ErrorMessage="Please Enter a Valid From Email address" ControlToValidate="txtFrom" ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)
*"></asp:RegularExpressionValidator> <asp:Label ID="Label2" Style="left: 100px; position: absolute; top: 125px" runat="server">To: </asp:Label> <asp:TextBox ID="txtTo" Style="left: 200px; position: absolute; top: 125px"
runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="ToValidator1" Style="left: 100px; position: absolute; top: 425px" runat="server" ErrorMessage="Please Enter the Email To." Width="200px" Height="23px" ControlToValidate="txtTo"></asp:RequiredFieldValidator> <asp:RegularExpressionValidator ID="ToValidator2" Style="left: 100px; position: absolute; top: 450px" runat="server" ErrorMessage="Please Enter a Valid To Email address" ControlToValidate="txtTo" ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)
*"></asp:RegularExpressionValidator> <asp:Label ID="Label3" Style="left: 100px; position: absolute; top: 150px"
runat="server">Subject</asp:Label> <asp:TextBox ID="txtSubject" Style="left: 200px; position: absolute; top: 150px" runat="server"></asp:TextBox> <asp:Label ID="Label4" Style="left: 100px; position: absolute; top: 175px" runat="server">Mail:
</asp:Label> <textarea runat="server" id="txtContent" style="left: 200px; width: 400px; position: absolute; top: 175px; height: 125px" rows="7" cols="24"> </textarea> <asp:Button ID="btnSend" Style="left: 200px; position: absolute; top: 350px" runat="server" Text="Send" OnClick="btnSend_Click"></asp:Button> <asp:Label ID="lblStatus" Style="left: 250px; position: absolute; top: 350px" runat="server"> </asp:Label> </form> </body> </html>
private void btnSend_Click(object sender, System.EventArgs e) { MailMessage msg = new MailMessage(); msg.To = txtTo.Text; msg.From = txtFrom.Text; msg.Subject = txtSubject.Text; msg.Body = txtContent.Value; lblStatus.Text = "Sending..."; SmtpMail.SmtpServer = "smtp.yourISP.com"; SmtpMail.Send(msg); lblStatus.Text = "Sent email (" + txtSubject.Text + ") to " + txtTo.Text;
{
StrFilePath = "c:\\emps.txt"; MailAttachment attach = new MailAttachment(strFilePath); msg.Attachments.Add(attach); .... }
Listing 1: Send an email from ASP.Net Page.
<%@ Import Namespace="System.Web.Mail" %>
<script language="c#" runat="server">
lblStatus.Text = "Sending...";

Figure 1 : Email Form in action
Figure 2: Results
}
Nevil patelPosted May 20, 2014, 2:34 PM
Well Done. Sending email is most common feature of application and this is how you can implement it.<a href="http://www.technonutty.com/2011/03/how-to-send-email-in-asp.net.html">how to send email using vb.net
manoj shettyPosted Jan 7, 2013, 5:04 AM
The server rejected the sender address. The server response was: 530 5.7.0 Must issue a STARTTLS command first. sg7sm1013842pbb.3 i got this error
Rajkamal SPosted May 25, 2012, 2:12 AM
The type or namespace name 'MailMesage' does not exist in the namespace 'System.Web.Mail' (are you missing an assembly reference?)why wht i do?????????
Ak JeelaniPosted Oct 27, 2011, 7:11 AM
Hi I am getting error in the program can any one help me.The error in the program is "SendUsing" configuration value is invalid.Can Any one tell me how to over come this problem please mail me to [email protected]
Biswajit PatraPosted Jul 13, 2011, 8:32 AM
good article
murali muraliPosted Jul 12, 2011, 11:31 AM
[send using configuration is invalid]........... This is the message i am geeting when used the above code........can u figure out why it happened? pls suggest me the alternatives.......thanks in advance.........
Mohd ShahnawazPosted May 31, 2011, 3:38 AM
I have 100 mail-Id in my database. I want to listing of undelivered Email-ID in my Gridview using Asp.net(C#).
michael meyerPosted Mar 14, 2011, 7:42 AM
Use MailMessage and SmtpClient class to build the sending mail program. I already answered in other website. See the below link : http://www.hyperneed.com/ShowSearchAnswers.aspx?searchstring=&category=Programming&questionid=b507b018-e456-4be6-b037-ec416c9eb712
thuong HoangPosted Mar 9, 2011, 10:31 AM
thanks you (^-^)
Suyog PatilPosted Jan 31, 2011, 3:54 AM
Its really good ... works very fast... Need to know one thing.If am writing Mail from, lets say [email protected]. Why it is not seen in sent mail of [email protected]
techie spidiePosted Oct 17, 2010, 5:43 PM
This Code helped ME: http://www.techiespider.com/?p=7
Aysenur HatipogluPosted Apr 27, 2010, 2:09 AM
This is a good article.
lahiru premasinghePosted Aug 25, 2009, 4:19 AM
thanx for ur tutorial
Mohamed Alie Pussah IIPosted Jun 20, 2009, 6:44 PM
How can I deploy an asp.net website with membership and role features so that it will work fine on production? I am completely new; forgive me!
Dorababu MekaPosted Jun 10, 2009, 8:09 AM
I am getting the following error for the program The "SendUsing" configuration value is invalid. Can you tell why
avaneet kumarPosted Feb 2, 2009, 12:31 AM
Do you want to send emails to multiple recipients? Visit following article: http://www.etechplanet.com/post/2008/11/21/How-to-send-Email-in-an-ASPNet-web-application-using-C.aspx
a bPosted Mar 21, 2008, 1:49 AM
hi i m a beginner in c# and asp.net and i want to send e-mail to outlook on click of a button using asp,c#,.net reply please