Microsoft ASP.NET Web Matrix Starter Kit (Bpg-Other)

Now that you understand the basic procedure for sending an e-mail message from a Web page, we can try some variations. The first variation will be to modify the Guestbook page so that whenever someone makes a new Guestbook entry, you receive an e-mail alert telling you about the update. The e-mail alert tells you who made the entry and what the Guestbook entry says. Figure 15-3 shows what the e-mail alert might look like when you receive it.

Figure 15-3: An e-mail message alerting you to a new Guestbook entry.

To illustrate how to send e-mail alerts, you ll modify the Guestbook page that you created in Chapter 10.

Modify the Guestbook page to send e-mail alerts

  1. Open the  Guestbook.aspx page that you created in Chapter 10, and save it under the new name  Guestbook_Email.aspx.

  2. In the new  Guestbook_Email.aspx page, switch to All view, and add the following declaration as line 2 of the page:

    <%@ import Namespace="System.Web.Mail" %>

  3. Switch to Design view, and double-click the Sign! button to create a Click handler for the button. Web Matrix switches to Code view.

  4. In the buttonSign_Click handler, find the following lines, which will be in a Try block:

    AddGuestbookEntry(EntryDate, who, e- mail, city, comment labelMessage.text = "Thanks for signing my guestbook! buttonSign.Enabled = False

    These lines perform the task of inserting the user s entry into the Guestbook table in the database.

  5. Immediately following the line buttonSign.Enabled = False, add the following code:

    Dim emailMessage As MailMessag Dim messageBody As Strin Dim signedName As Strin emailMessage = New MailMessag If textName.Text = "" The     signedName = "(Anonymous user) Els     signedName = textName.Tex End I If textEmail.Text = "" The     emailMessage.From = "mikepope@contoso.com Els     emailMessage.From = textEmail.Tex End I emailMessage.To = "mikepope@contoso.com emailMessage.Subject = "New guestbook entry messageBody = "Someone named <b>" & signedName & "</b> messageBody &= "just added the following comme nt "   & "to your guestbook.<hr> messageBody &= textComment.Tex emailMessage.Body = messageBod emailMessage.BodyFormat = MailFormat.HTM SmtpMail.SmtpServer="localhost Tr     SmtpMail.Send(emailMessage Catc End Try

The code you just added to the page is similar to the code you wrote for the  SendEmail.aspx page earlier in this chapter. You again create an e-mail message as a MailMessage object and set its To, From, Subject, and Body properties. You send the e-mail message as you did in the previous example, by calling the SmtpMail.Send method. The differences between the Guestbook alert code and the code you wrote for the  SendEmail.aspxpage are as follows:

Test the  Guestbook_Email.aspx page by running it and entering a value for each field. When you click the Sign! button, you ll see a thank- you message. Behind the scenes, of course, the page sends you an e-mail message that alerts you to the new guestbook entry. Check your e-mail sooner or later, you ll get the message.

You can probably imagine other uses for e-mail alerts. For example, you can add e-mail alerts to the calendar application we created in Chapter 14; perhaps you can have the application send you an e-mail message when someone in your family updates the calendar. Let s move on to examining another way you can use e-mail in your applications.

Категории