Sending E-Mail Alerts from Your Guestbook


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.

click to expand
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:

  • In the Guestbook alert e-mail page, you set the e-mail object s From property to either the signer s e-mail address (if the signer provided an e-mail address) or to your own e-mail address (if the signer didn t provide an e-mail address). This ensures that even if users don t provide an e-mail address, the message still has a return address.

  • In the Guestbook alert page, the e-mail message is always sent to you the e-mail message s To property is hard-coded to your e-mail address. In the  SendEmail.aspx page, users specified who they wanted to send e-mail messages to.

  • In the Guestbook alert page, the body of the message is concatenated from the signer s name (if the signer provided one), a standard introduction, and then the guestbook entry itself. Notice that the text includes some HTML formatting. The Guestbook e-mail alert is formatted as HTML. In the  SendEmail.aspx page, we didn t specify any kind of formatting for the e-mail message, so the message is sent as plain text. But by setting the e-mail object s BodyFormat property to the predefined value MailFormat.HTML, you can send an e-mail message formatted as HTML. You can then include HTML formatting in the message. For example, in the Guestbook alert page, we include a <b> and an <hr> element.

    Note 

    I didn t apply HTML formatting to e-mail messages in the  SendEmail.aspx page because the text of the e-mail message in that page comes from users and would therefore be subject to script exploits. In the Guestbook alert page, however, the body text of the e-mail message is created in code, so you can safely format the message as HTML. I discuss a way to allow some HTML formatting in public Web pages in Appendix A.

  • Although the SmtpMail.Send method is contained in a Try-Catch block in case an error occurs, I don t do anything in the Catch block. If the Guestbook entry page can t send an e-mail message, the page shouldn t show an error to the Guestbook user why would the Guestbook user care about your e-mail problems? So if an error occurs while the e-mail alert is being sent, the send process fails silently and you simply don t get an alert. You still want to have a Try-Catch block, however, because without Try-Catch, if an error occurs during the send process, the page will crash.

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.




Microsoft ASP. NET Web Matrix Starter Kit
Microsoft ASP.NET Web Matrix Starter Kit (Bpg-Other)
ISBN: 0735618569
EAN: 2147483647
Year: 2003
Pages: 169
Authors: Mike Pope
BUY ON AMAZON

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