Main Points
- C# Code Behind The actual C# code behind.
- General C# Structure The basics of the C# structure and event handlers.
- Message Sending Subroutine The explanation of the code which constructs and sends the email.
- Skip to Main Points links return to the Main Points menu.
C# Code Behind
using System.Net;
using System;
{
}
{
MailMessage objMail = new MailMessage(); // the email (technically mail message)
NetworkCredential objCredentials = new NetworkCredential("admin@mywebsite.com", "superSecretPassword"); // the authentication credentials
SmtpClient objSend = new SmtpClient("mail.mywebsite.com", port#); // the SMTP account
Uri objUrl = Request.Url; // the URL of the current webpage (this subroutine will run on every page of the website, so we don't know if we don't go grab it)
objMail.From = new MailAddress("Admin <admin@mywebsite.com>");
objMail.To.Add(new MailAddress("contact@mywebsite.com"));
objMail.To.Add(new MailAddress("anotheremail@mywebsite.com"));
objMail.Subject = "I have a question or comment.";
objMail.IsBodyHtml = true;
objMail.Body = "Hi!<br /><br />You have a message from an Earth Chronicle visitor. They have left this information after visiting...<br /><br />";
objMail.Body += "<a href='" + objUrl.AbsoluteUri + "'>" + objUrl.AbsoluteUri + "</a><br /><br />";
objMail.Body += "email: " + this.tbEmail.Text + "<br /><br />They write...<br /><br />" + this.tbComments.Text + "<br /><br />TTFN,<br />the Server";
objSend.UseDefaultCredentials = false;
objSend.Credentials = objCredentials;
objSend.Send(objMail);
this.lblSent.Text = "Sent.";
General C# Structure
using System.Net;
using System;
The first portion of the C# code behind sets the namespaces needed, just like the VB code behind. This code uses classes from three namespaces which are indicated by the using statements.
}
The next code block defines the class. This class is a webpage as noted by its namespace System.Web.UI.Page. It is public because it needs to be accessible to anyone or any computer. It is a partial class because the main webpage contains more information about how this webpage is to be constructed. And it's name is the name of the webpage, ContactFormTest06CSharp.
{
}
{
}
Inside the ContactFormTest06CSharp class we have two event handlers which define it's responses. A C# event handler starts with protected, because it's functionality is largely inaccessible except to this webpage. Void is a C# keyword which means that this is a fake function. Functions return a data type, but in this case the "function" in question doesn't return anything at all. It simply executes a set of actions, hence void. Next we specify the name/reason for the function call. The first event handler is called when the webpage loads, Page_Load, big surprise. Nothing happens on Page_Load currently, but since that's a very common event to tie into, we may well need it at some point. Hence, I felt no great burden to get rid of it (we're also using it a little in behind the scenes testing). The second function is triggered when the form button btnEmail is clicked. This is the interesting one, but that pretty much covers the basics of C# structure in ASP.NET 2.0.