ASP.NET Sample Programs with Source Code
Creating Your First ASP.NET Application Program:
For your first sample ASP.NET projects, As you will see, ASP.NET is very easy, and you will be up and running in no time at all.
1. Start a new document in either Visual Studio.NET or the text editor of your choice.
2. Enter the code into the document, and then go to File -> Save As and name it HelloWorld.ASPX in your Web root folder.
3. Launch your Web browser and enter the location of the new file (such as- localhost/helloworld.aspx).
2. Enter the code into the document, and then go to File -> Save As and name it HelloWorld.ASPX in your Web root folder.
3. Launch your Web browser and enter the location of the new file (such as- localhost/helloworld.aspx).
Program:
<html> <head> <title>Example 1: Hello World</title> </head> <body bgcolor=white> <h1> <% response.write("Hello World") %> </h1> </body> </html>
Sending E-mail Program in ASP.NET:
<%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”SendMail.aspx.cs” Inherits=”SendMail.SendMail” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”> <html xmlns=”https://www.w3.org/1999/xhtml”> <head runat=”server”> <title></title> </head> <body> <form id=”form1″ runat=”server”> <div> <table style=”width: 56%;” align=”center”> <tr> <td align=”center” colspan=”2″ width=”90%”> <strong>How to ASP.NET Sending Email</strong></td> </tr> <tr> <td align=”right”> From:</td> <td align=”left”> <asp:TextBox ID=”txtFrom” runat=”server”></asp:TextBox> </td> </tr> <tr> <td align=”right”> To:</td> <td align=”left”> <asp:TextBox ID=”txtTo” runat=”server”></asp:TextBox> </td> </tr> <tr> <td align=”right”> Subject</td> <td align=”left”> <asp:TextBox ID=”txtSubject” runat=”server”></asp:TextBox> </td> </tr> <tr> <td align=”right”> Message</td> <td align=”left”> <asp:TextBox ID=”txtMsg” runat=”server” TextMode=”MultiLine”></asp:TextBox> </td> </tr> <tr> <td align=”center” colspan=”2″> <asp:Button ID=”btnSend” runat=”server” onclick=”btnSend_Click” Text=”Send” /> </td> </tr> </table> </div> </form> </body> </html>
File: SendMail.apsx.cs
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Net.Mail; namespace SendMail { public partial class SendMail:System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { this.btnSend.Click += new EventHandler(this.btnSend_Click); } protected void btnSend_Click(object sender, EventArgs e) { MailMessage emailMsg = new MailMessage(txtFrom.Text, txtTo.Text); emailMsg.Subject = txtSubject.Text; emailMsg.Body = txtMsg.Text; SmtpClient client = new SmtpClient(“smtp.gmail.com”, 587); client.EnableSsl = true; client.Credentials = new System.Net.NetworkCredential(“[email protected]”, “YourPassword”); try { client.Send(emailMsg); } catch (Exception ex) { Console.WriteLine(“Exception caught in SendEmailMessage(): {0}”, ex.ToString()); } } } }
File: SendMail.apsx.designer.cs
<auto-generated> </auto-generated> namespace SendMail { public partial class SendMail { <summary> </summary> <remarks> </remarks> protected global::System.Web.UI.HtmlControls.HtmlForm form1; <summary> </summary> <remarks> </remarks> protected global::System.Web.UI.WebControls.TextBox txtFrom; <summary> </summary> <remarks> </remarks> protected global::System.Web.UI.WebControls.TextBox txtTo; <summary> </summary> <remarks> </remarks> protected global::System.Web.UI.WebControls.TextBox txtSubject; <summary> </summary> <remarks> </remarks> protected global::System.Web.UI.WebControls.TextBox txtMsg; <summary> </summary> <remarks> </remarks> protected global::System.Web.UI.WebControls.Button btnSend; } }
Output: