Main Points

C# Code Behind

using System.Net.Mail;
using System.Net;
using System;
public partial class ContactFormTest06CSharp : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnEmail_Click(object sender, EventArgs e)
{
// Declaring necessary mail variables
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)
// the email configuration information: sender, recipients, subject
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;
// the body of the email
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";
// SMTP configuration information and use of Send method
objSend.UseDefaultCredentials = false;
objSend.Credentials = objCredentials;
objSend.Send(objMail);
// notify the visitor that the procedure has successfully completed
this.lblSent.Text = "Sent.";
}
}
Skip to Main Points

General C# Structure

using System.Net.Mail;
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.

public partial class ContactFormTest06CSharp : System.Web.UI.Page
{

}

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.

protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnEmail_Click(object sender, EventArgs e)
{

}

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.

Skip to Main Points

Message Sending Subroutine

// Declaring necessary mail variables
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)

The first thing we do in this subroutine which sends email, is declare our variables. These are slightly different in syntax, but otherwise identical to declaring variables in VB. It strongly helps that each data type is class from the .NET libraries. This let's us use statements which take advantage of the 3rd tier in .NET architecture, their strength is that they are virtually the same across different languages. They rely on fundamentally defined classes in the .NET class libraries so that when we adapt them from VB to C# we basically only add semicolons to the ends of the statements. This is a key advantage of 3rd tier development, it gives unprecedented efficiency to developing legacy systems.

// the email configuration information: sender, recipients, subject
objMail.From = new MailAddress("Admin ");
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;

With the addition of semicolons to the ends of each line and switching IsBodyHtml from "True" to "true", these statements are otherwise identical to the VB code. Again, this is all 3rd tier code, very easy to update.

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";

The third code block defines the contents of the email message. The real meat. Note that we use our objUrl to create a link to the webpage which the visitor sent the email from. That way if they are referring to anything on that page, we can check it out easily.

// SMTP configuration information and use of Send method
objSend.UseDefaultCredentials = false;
objSend.Credentials = objCredentials;
objSend.Send(objMail);

This is the last code block which handles mail variables. It uses the SMTPClient object to send the email. The first two lines of the Code define where objSend should be looking for information to log into the system, ie objCredentials. The final line sends our MailMessage, objMail.

// notify the visitor that the procedure has successfully completed
this.lblSent.Text = "Sent.";

Finally, since the visitor still has no idea what's on, so we create a quick sent message to notify the visitor that everything went beautifully.

Skip to Main Points
<-- Back to 5. Contact Form - Email Tuning
Continue to ASP.NET 2.0 Play-->