Using the ASPEmail Component
by Sharon Huston

A component is a specialized program that can interact with other programs. The Windows "File Open" dialog box most programs use to open files is an example of a component. Authors of various programs (Photoshop or Dreamweaver, for example) do not have to write their own file opening routines -- instead the authors make their program communicate with one of Microsoft's file management components. This saves programmers considerable time and effort.

Components can perform several tasks for web developers. BrowserHawk, for example, can perform very complicated browser detection. Other components can upload files, spell-check documents, and perform other tasks.

ASPEmail, the component we will be working with today, is a free component. Components must be installed on the server by server administrators. ASPEmail will only work with Windows servers, or with Unix servers running ChiliSoftASP. Components like ASPEmail are often used for routine tasks, like emailing password reminders or emailing users a copy of their order. These two examples are fairly complicated, and work best when used with a database.

In this scenario we'll do something a little easier -- we'll construct a simple Customer Support email system. We'll build a form that collects information from the user, and then send the data to a new page which will compile the information and send it in an email.

More information on ASPEmail is available on the developer's website.

Creating the Form

Names and capitalization are VERY important in this exercise. Do yourself a favor and name your files, form items, and variables EXACTLY as they are named below.

  1. Place a form on a blank page.
  2. Select the form (either by clicking on the red outline or by using the tag selector) and change the following form properties:
    • Change form name to getinfo.
    • Set the form's action to sendmail.asp.
    • Change form's method to Post.
    • Insert a table inside the form with six rows and two columns. Set the table border to zero.

    In the past few exercises we've used the GET method, which appends the data to the querystring. Get has a limitation we have not discussed -- in older browsers, the Get menthod is limited to 1 KB of data. Since an email could potentially have more characters we are switching to the POST method. In older browsers Post can handle up to 4KB of data. Data sent using Post is not appended to the querystring. (note: In newer browsers there are no size limits for POST and GET.)

  1. Examine the following illustration:
  2. Inside the form, place the following form elements as shown in the illustration.
    • A text field named firstName
    • A text field named lastName
    • A tex field named emailAddress
    • A list/menu named emailTopic, with the following list values:
      Item Label Value
      I Love Your Website I Love Your Website
      I Hate Your Website I Hate Your Website
      I Have Trouble With Your Website I Have Trouble With Your Website
      Elvis Lives Elvis Lives
      Other Other

      **Make I Love Your Website the default value.

    • A text field named emailBody, which is multiline (5 lines) and 80 characters wide.
    • A Submit button. Change the button's label to Send Message.
  3. Save the page to your xhtml folder as getinfo.htm.
  4. Upload the page.

Creating The Processing / Display Page

  1. Open a new, blank page in Dreamweaver.
  2. Switch to CODE VIEW.
  3. Place this line at the very top of the page, above the <html> tag:

    <%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>

  4. Our first task is to retrieve the post data so we can use it in our program. In the past we've performed similar tasks using the Request.Querystring command. This command won't help us now because we didn't append the data to the querystring. To retrieve POST data, we must use Request.Form instead. Place the following code block between the </head> and <body> tags:

    <%
    firstName = Request.Form("firstName")
    lastName = Request.Form("lastName")
    emailAddress = Request.Form("emailAddress")
    emailTopic = Request.Form("emailTopic")
    emailBody = Request.Form("emailBody")
    %>

  5. Let's check our work using the Response.Write command covered in previous assignments. Add the following code block between the <body> and </body> tags:

    <%
    response.write firstName & "<br>"
    response.write lastName & "<br>"
    response.write emailAddress & "<br>"
    response.write emailTopic & "<br>"
    response.write emailBody & "<br>"
    %>

  6. Save the page to your xhtml folder, naming it sendmail.asp.
  7. Upload the page.
  8. Using your web browser, visit the uploaded getinfo.htm page. You must be visiting this page live off the web server -- not off your hard drive.
  9. Fill out the form, and click the submit button. Don't leave any of the fields blank -- this will cause problems. If everything goes well the browser should send you to your sendmail.asp page, which will display the data from the form.
  10. Now it's time to make the page actually send email. The following code is sample code from the ASPEmail website. Please paste this code below your Request.Form code block.

    <%
    Set Mail = Server.CreateObject("Persits.MailSender")
    Mail.Host = "smtp.smtp-server.com" ' Specify a valid SMTP server
    Mail.From = "sales@veryhotcakes.com" ' Specify sender's address
    Mail.FromName = "VeryHotCakes Sales" ' Specify sender's name
    Mail.AddAddress "andy@andrewscompany.net", "Andrew Johnson, Jr."
    Mail.AddAddress "paul@paulscompany.com" ' Name is optional
    Mail.AddReplyTo "info@veryhotcakes.com"
    Mail.AddAttachment "c:\images\cakes.gif"
    Mail.Subject = "Thanks for ordering our hot cakes!"
    Mail.Body = "Dear Sir:" & Chr(13) & Chr(10) & _
    " Thank you for your business."
    On Error Resume Next
    Mail.Send
    If Err <> 0 Then
    Response.Write "Error encountered: " & Err.Description
    End If
    %>

  11. This is a very large block of code! We'll examine it a few lines at a time, and customize it so it works in our environment.
  12. The first line, Set Mail = Server.CreateObject("Persits.MailSender"), creates an instance of the ASPEmail component for our page to use. An "instance" is basically a copy of the component that a particular page works with. Component instances allow several pages to use the same component at the same time. Do not alter this line.
  13. Next comes Mail.Host = "smtp.smtp-server.com" ' Specify a valid SMTP server. This line tells the component the address of the mail server. Notice the apostrophe (') in this line. Everything after the apostrophe is a comment -- a little reminder for the programmers. Comments do not affect the code. The comment in this line is explaining we need to use a valid SMTP (Simple Mail Transfer Protocol) server. We need to modify this line to use our server:

    Mail.Host = "nlecommerce1.dcccd.edu" 'Specify a valid SMTP Server

  14. The next two lines deal with the message's sender -- the person who filled out the form. They currently read

    Mail.From = "sales@veryhotcakes.com" ' Specify sender's address
    Mail.FromName = "VeryHotCakes Sales" ' Specify sender's name

    We need to change these lines so they use the data from our form. Change them to read

    Mail.From = emailAddress ' Specify sender's address
    Mail.FromName = firstName + " " + lastName ' Specify sender's name

    The first line will now contain the user's email address. The second line is a little more complicated. If you remember, we asked for the user's first name in one form field and their last name in a second form field. We're going to concatonate (join together) these fields so that Mail.FromName is equal to the user's first and last name -- for example, "Nancy Drew." We have to add a space between the two field variables so we don't end up with NancyDrew. If you remember, we inadvertently concatonated numbers in our calculator so that work + bench = workbench.

  15. The next two lines deal with the person the message is being sent to -- in this case, your fictional Customer Support department. The lines currently look like this:

    Mail.AddAddress "andy@andrewscompany.net", "Andrew Johnson, Jr."
    Mail.AddAddress "paul@paulscompany.com" ' Name is optional

    This message is being sent to two people -- andy@andrewscompany.net and paul@paulscompany.com. We want to send our message to your Customer Service department (you) and also send a courtesy copy to the person who filled out the form, so they have a copy for their records. Modify the lines as in the example below, using your email address in the second line.

    Mail.AddAddress emailAddress, firstName + " " + lastName
    Mail.AddAddress "MyAddress@wherever.com" ' My email address

  16. The next line, Mail.AddReplyTo "info@veryhotcakes.com" can be a little hard to understand. In a typical email program, pressing a "reply" button will send the email to the person who wrote it. The email program knows who to send the answer to by examining information in the email's header. It is possible to change the header's reply-to field so that the message is sent to someone else. Since we don't really need to send the reply to someone else, modify the line so it uses your email address as in the example below.

    Mail.AddReplyTo "MyAddress@wherever.com" ' My email address

  17. The following line, Mail.AddAttachment "c:\images\cakes.gif" is used to add an attachment to the email message. This is a feature available with the non-free version of ASPEmail, which Northlake does not own. We can't use this line, so you can either delete it, or comment it out by placing an apostrphe at the beginning of the line, like this:

    ' Mail.AddAttachment "c:\images\cakes.gif"

  18. Time to test -- save the document, upload, and use a web browser to visit the getinfo.htm page. Fill out the form. Check your email. We haven't modified the email yet, so you should have a message with "Thanks for ordering our hot cakes!" as the subject.
  19. Back to the code. The next line is used to set the email's subject. It currently reads

    Mail.Subject = "Thanks for ordering our hot cakes!"

    We need to change this so it reads

    Mail.Subject = emailTopic

  20. The message body takes up two lines. It reads

    Mail.Body = "Dear Sir:" & Chr(13) & Chr(10) & _
    " Thank you for your business."

    If you look at your test email you'll see it looks like this:

    Dear Sir:

    Thank you for your business.

The text in quotes "Dear Sir:" and "Thank you for your business." appear in the email seperated by a blank line. In the past we've concatonated text using the plus (+) sign. It's also acceptable to use an ampersand (&) as this example does. The blank line is created by the Chr(13) & Chr(10) part of the code.

Typically, ASP commands are written one command per line. The underscore (_) is used when the command takes more than one line. The underscore lets the program know the following line(s) is also part of the command.

We want to modify this code so a short message appears, informing the user that this message is a copy of the message they sent from your site, that the message was sent on a specific date, and that the user can expect a reply in two working days.

We'll use the date command to get the current date from the system.

Modify these two lines so that they look like this:

Mail.Body = "Thanks for sending our Customer Support " & _
   " team a message. A reply, if required, will be sent in " & _
   "the next forty-eight hours. Your original message was " & _
   " sent on " & date & Chr(13) & Chr(10) & _
   " -----Original Message--------" & chr(13) & chr(10) & _
    emailBody

  1. The remainder of the code involves error trapping, and is outside the scope of our lesson. We'll leave it alone.
  2. You're finished -- save the document, upload, and use a web browser to visit the getinfo.htm page. Fill out the form and check your email.