Part V: Building the Topic List and Sending Mail

In this part of the project, you will create the list of topics to appear in the drop-down list box on the home page. Use ADO to read the database and create the list.

Step 1

If you don't already have the EMailInfo web open in Visual InterDev, open it now. Open DEFAULT.ASP in Visual InterDev. Identify the <SELECT> tag that creates the drop-down list box.

Add an <OPTION> </OPTION> tag pair for each topic in the database. These tags are used to denote items in the list. Add the following code between the <SELECT> and </SELECT> tags:

<% ` Variables Dim objConnection Dim objRecordset ` Open a connection Set objConnection = Server.CreateObject("ADODB.Connection") objConnection.Open "EMailInfo" ` Get entries Set objRecordset = Server.CreateObject("ADODB.Recordset") objRecordset.Open _     "SELECT ItemID, ItemDescription FROM Items", objConnection ` Build the list. ` The description is shown in the list, ` but the ItemID is returned in code ` so that you can search the database. Do While Not objRecordset.EOF%>     <OPTION VALUE=<%=objRecordset("ItemID")%>>     <%=objRecordset("ItemDescription")%>     </OPTION><%     objRecordset.MoveNext Loop ` Close the connection objRecordset.Close objConnection.Close Set objRecordset = Nothing Set objConnection = Nothing %> 

Save your work.

Step 2

Preview DEFAULT.ASP in Internet Explorer. You should see the topics in the drop-down list box, as shown in Figure 9-4.

Figure 9-4. The topic list.

Step 3

Now that the information form is complete, you are ready to send the mail. Add a new page to your project by choosing New from the File menu. In the New dialog box, add a new ASP page, name it MAIL.ASP, and click OK.

Step 4

To send the mail, use the E-Mail component you created earlier. Because the component was written in Visual Basic 5.0, it is already properly registered and ready to use. You create an instance of this component in the same way that you create an instance of any component—by using the CreateObject method of the Server object. Add the following code to the BODY section of MAIL.ASP to access the E-Mail component:

<% ` Variables Dim objMail Dim objConnection Dim objRecordset Dim blnSuccess blnSuccess = False ` Create the E-Mail component Set objMail = Server.CreateObject("VBMail.Connector") ` Create database objects Set objConnection = Server.CreateObject("ADODB.Connection") Set objRecordset = Server.CreateObject("ADODB.Recordset") ` Get the item to send objConnection.Open "EMailInfo" objRecordset.Open _     "SELECT ItemDescription, ItemText FROM Items WHERE ItemID =" _     & Request.Form("lstTopic"), objConnection If Not objRecordset.EOF Then ` Set data into Mail component objMail.Sender = Application("Sender") objMail.Recipient = Request.Form("txtEMail") objMail.SenderName = Application("SenderName") objMail.RecipientName = Request.Form("txtFirstName") _     & " " & Request.Form("txtLastName") objMail.Subject = objRecordset("ItemDescription") objMail.Body = objRecordset("ItemText") objMail.Server = Application("Server") ` Send mail! blnSuccess = objMail.Send End If If blnSuccess Then%> <H1>Thank You!  Your mail was sent.</H1> <%Else%> <H1>We're sorry!  There is a problem sending mail.   Please check your information.</H1> <%End If%> 

Step 5

In the code for the E-Mail component, you specified several key properties using Application variables. These variables provide a simple way of customizing the application for your e-mail server and information topics. To set up these variables in the GLOBAL.ASA file, open the file in Visual InterDev. GLOBAL.ASA traps four important events in your project: Session_OnStart, Session_OnEnd, Application_OnStart, and Application_OnEnd. Here is the code:

<SCRIPT LANGUAGE="VBScript" RUNAT="Server">     Sub Application_OnStart         Application("Sender") = "[Your Company E-Mail]"         Application("Server") = "[Your E-Mail Server]"         Application("SenderName") = "[Your Company Name]"     End Sub </SCRIPT> 

Save your work.

Step 6

Test your application. You should now be able to send e-mail!


NOTE: This application requires that all form fields be filled out! The user can crash the application by not providing information for all fields in DEFAULT.ASP. Add data validation to the form later as an extra exercise.



Programming Active Server Pages
Programming Active Server Pages (Microsoft Programming Series)
ISBN: 1572317000
EAN: 2147483647
Year: 1996
Pages: 84

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net