Main Points

VB Code Behind

Imports System.Net.Mail
Partial Class ContactFormTest05EmailTuning
Inherits System.Web.UI.Page
Protected Sub btnEmail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEmail.Click
Dim objMail As New MailMessage
Dim objUrl As Uri = Request.Url
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: " & Me.tbEmail.Text & "<br />They write...<br /><br />" & Me.tbComments.Text & "<br /><br />TTFN,<br />the Server"
Dim objSend As New SmtpClient("mail.mywebsite.com", port#)
Dim objCredentials As New Net.NetworkCredential("admin@mywebsite.com", "superSecretPassword")
objSend.UseDefaultCredentials = False
objSend.Credentials = objCredentials
objSend.Send(objMail)
Me.lblSent.Text = "Sent."
End Sub
End Class
Skip to Main Points

General VB Structure

Imports System.Net.Mail

This page looks exactly like the previous one. So what changed? The VB code behind has been updated to do what we need it to do. The first line, Imports, declares a namespace which has information relevant to this code. A visitor fills out the contact form and clicks the "Email Us!" button calling this code behind. Since the code will handle all the issues with sending an email, it's easier to declare the Mail namespace once in imports so you don't have to do it over and over again.

Partial Class ContactFormTest05EmailTuning
Inherits System.Web.UI.Page

Next the code defines the name of a new partial class called ContactFormTest05EmailTuning. This is only a partial class because the main page, *.aspx, also contains part of the class information; these two files have to be combined to make a full class (that's what the compiler does when it builds the website). Note that the very last line is an End Class statement. The 3rd line, Inherits System.Web.UI.Page, removes any doubt about what kind of class this is, it's a webpage.

Protected Sub btnEmail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEmail.Click

The next line is a monster but it has two key parts. Protected Sub btnEmail_Click, is the first. Protected is a keyword that makes the following code mostly unaccessible to outside objects. The webpage can access it, this code is defining it's class, but it's difficult for another webpage or object to get at it. Sub indicates that this is a subroutine, note that the second to the last line is End Sub. This is followed by the name of the subroutine. We could name it Sub Fred, if we wanted. However, .NET's default name, btnEmail_Click is descriptive of the circumstances in which this subroutine is called; it's a good name.

The other important part of the line is Handles btnEmail.Click. Handles is a keyword in VB that describes the events which trigger this subroutine. You can list many, however, in this case we only want to call the subroutine when someone clicks the button named btnEmail. Everything that falls between this line and End Sub is the set of instructions called when someone clicks the button.

Skip to Main Points

Message Sending Subroutine

Dim objMail As New MailMessage
Dim objUrl As Uri = Request.Url

The first statements in the subroutine create the variables needed. Dim is the keyword used to declare a variable in VB. This is followed by the variable name which could be anything. "As" is another VB keyword, it describes what data type the variable is part of, a letter, a number, or whatever. objMail is defined as a MailMessage, further, the "New" keyword creates a usable instance of the MailMessage and puts it in objMail. Next we create objUrl which is defined as a Uri data type. The syntax is different, but once again, we set the value of the variable, in this case we use Request.Url to grab the Url of the current webpage and put it in objUrl.

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

Now that we have two powerful variables, we can start to use them. The second block of code sets all the properties of the email necessary to send it. objMail.From is the property of objMail which states who is sending the email. objMail.To.Add is the property which states who the email will be sent to; notice that we can define as many recipients as we like. We also define the subject of the email, and declare that the email IS written in HTML.

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: " & Me.tbEmail.Text & "<br />They write...<br /><br />" & Me.tbComments.Text & "<br /><br />TTFN,<br />the Server"

The third block of code defines the message, the body of the email. The first line holds all the introductory greetings. Note that even though the message is from a server, it can still sound like a friendly letter. You just have to write a friendly letter instead of something that sounds like it's written by a computer. The second line takes advantage of our second variable. objUrl has the Url of the current page stored inside it. objUrl.AbsoluteUri is the full Url of the webpage as a string. http://www... etc. This is necessary, because we can only add a string (not a Url or Uri object) to the Body property of objMail. More than that we not only list the Url of the current page, but create a link so that the recipient of the email, in this case Earth Chronicle, can easily see the page the visitor was looking at when they decided to email us. The final line organizes the information that the visitor entered into the contact form and appends a conclusion.

So what is the Url of the current page... Well, it's pretty obvious in this case, it's the page you're looking at. We could have simply typed the page Url into the code. However, this is a more flexible way to get the URL. For our next step, this code will control each contact form on every page of the website. THEN we won't have a clue where the visitor will be emailing from, it could be any page. That's why we let the objUrl grab the Url at the time the visitor clicks the button. That way, we can not only know exactly where the visitor was when they emailed us, we can link directly there. So if someone has a question related to a web page, found an error, or has an addition, we can see exactly the same thing as we read their email.

Dim objSend As New SmtpClient("mail.mywebsite.com", port#)
Dim objCredentials As New Net.NetworkCredential("admin@mywebsite.com", "superSecretPassword")
objSend.UseDefaultCredentials = False
objSend.Credentials = objCredentials

The next two short blocks configure the email settings to actually send the message (hence why they come last). In the first block we create two new variables. objSend is an object of type SmtpClient; this is the object that will actually send our email. Note that it has two parameters needed by the server. The email domain and the port number. The domain is specified by the web host, and while the port number for SMTP is typically 25, many web hosts change this to give their servers increased security against outside intruders. objCredentials is obviously the user name and password that the mail server recognizes to allow the email to be sent. The second block of code, uses two properties of objSend which explicitly state that objCredentials has the credentials to authorize the email.

objSend.Send(objMail)
Me.lblSent.Text = "Sent."

Last but not least, objSend.Send, no surprise here, actually sends an email message. In our case that's objMail, the MailMessage object. Finally, all this code has happened on the server. This is wonderful and powerful. However, the visitor has absolutely no indication that any of this has happened. And for the most part that's ok. The visitor almost certainly doesn't want to know that we're authorizing the email to be sent or declaring that the email is written in HTML. They don't care. However, they most certainly do care that their email was sent. So in our last line we set the text property of an empty label to "Sent." which gives the visitor a clear message that everything has gone OK. That's why this is the last step; if the code fails to execute, or fails to execute completely, the Sent text is never written. If it's written, the visitor can feel safe that their message has gone through.

Skip to Main Points
Continue to 6. Contact Form - C#-->