This documentation is archived and is not being maintained.

Microsoft Office FrontPage 2003 Inside Out

Office 2003
 

This article is an excerpt from Microsoft Office FrontPage 2003 Inside Out from Microsoft Press (ISBN 0-7356-1510-1, copyright Microsoft Press 2003; all rights reserved). The author, Jim Buyens, is a FrontPage MVP and a noted expert on Web programming and networking. He develops Web-based business systems for the telecommunications industry and has written several books, including Microsoft FrontPage 2002 Inside Out, Faster Smarter Beginning Programming, and Web Database Development Step by Step .NET Edition, all from Microsoft Press.

No part of these chapters may be reproduced, stored in a retrieval system or transmitted in any form or by any meanselectronic, electrostatic, mechanical, photocopying, recording or otherwisewithout the prior written permission of the publisher, except in the case of brief quotations embodied in critical articles or reviews.

Companion CD Content: Sending Custom E-Mail

Contents

Sending Mail with Active Server Pages
Sending Mail with ASP.NET

The Microsoft Office FrontPage 2003 Save Results component does an adequate job of sending the contents of a form as an e-mail message, but it's not flexible enough to handle all situations. You can't, for example, format the message exactly to your liking, and you can't send e-mail messages to different recipients depending on the circumstances. If you have these kinds of requirements, you'll need to locate other tools and do some custom coding. Two such tools are ASP and its more recent incarnation, Microsoft ASP.NET.

If your Web server isn't running a Microsoft Windows operating system or somehow has Collaboration Data Objects (CDO) disabled, ask the server's administrator whether another mail program is available and, if so, where to find the documentation. Sending e-mail is such a common requirement that nearly all Web servers provide a way to do it.

Sending Mail with Active Server Pages

ASP has no built-in way of sending mail, but it can easily do so using a Microsoft or third-party ActiveX control. The Microsoft ActiveX control is one of the CDO objects that comes with Microsoft IIS versions 4 and later. This set of objects sends and receives mail in the background, under the control of other programs. Installing the SMTP Server component of IIS installs the CDO objects as well.

The ASP code for making CDO send a message can be as simple as this:

<%
Set objNewMail = Server.CreateObject("CDONTS.NewMail")
objNewMail.From = "hannah@deadletter.msn.com"
objNewMail.To = "herman@deadletter.msn.com"
objNewMail.Subject = "Happy Birthday"
objNewMail.Body = "If your birthday is today, have a good one."
objNewMail.Send
Set objNewMail = Nothing
%>

The first line creates a CDO object for sending new mail. The next four lines assign from and to addresses, a subject line, and the body of the message. The command sends the message, and the last line destroys the CDO object.

Note   To check for the presence and version of CDO on your Web server, run the ASP self-diagnosis page in Insider Extra 1 or the ASP.NET self-diagnosis page in Insider Extra 2.

Among the Insider Extra files you installed from the companion CD, the email folder contains a simple Web page named mailer.asp that illustrates the use CDO to send mail from an HTML form. Figure IE9-1 shows this form.

Click here to see larger image

Figure IE9-1. ASP code in this Web page sends e-mail based on information the visitor enters.

CDO can attach files to any message it sends. To use this facility, add a statement such as this anywhere after the Set objNewMail statement and before the objNewMail.Send statement in the preceding example:

objNewMail.AttachFile(Source)

Source is the name of the file you want to attach. This must be a fully qualified file name in the Web server's file system.

To format a message as HTML, add the following statements anywhere after creating the objNewMail object and sending it:

objNewMail.MailFormat = 0
objNewMail.BodyFormat = 0

The first statement tells CDO to send the message in MIME format (that is, as one or more formatted parts). The second statement tells CDO to flag the body of the message as HTML. Of course, you would then need to store valid HTML—including <body> and</body> tags inside <html> and </html> tags—in the Body property.

Table IE9-1. Values for the MailFormat and BodyFormat Properties

Property Value Description
MailFormat 0 The message will contain one or more parts, each flagged with a file format. That is, it will be in MIME format.
  1 The message will contain uninterrupted plain text. This is the default.
BodyFormat 0 The Body property will include Hypertext Markup Language (HTML).
  1 The Body property will contain plain text only. This is the default.

Table IE9-1 lists the acceptable values for the MailFormat and BodyFormat properties.

To read this HTML from a file (a file you created in FrontPage, for example), use code like the following:

Dim fso
Dim htmlFile
Dim htmlText
Const ForReading = 1
Set fso = ServerCreateObject("Scripting.FileSystemObject")
Set htmlFile = fso.OpenTextFile(Server.MapPath("sample.htm"), _
    ForReading)
htmlText = htmlFile.ReadAll
htmlFile.Close
htmlFile = Replace(htmlFile, "##date1##", _
FormatDateTime(Now, vbLongDate))
objNewMail.Body = htmlText

In this code, the first four statements declare variables and constants. The fifth statement loads a Scripting.FileSystemObject object into memory, and the sixth uses that object to open a file named sample.htm located in the same folder as the ASP page that contains the code. To use a different file, specify a relative URL other than sample.htm. The Server.MapPath method converts a URL on the local server to a physical file name. The seventh and eighth statements (which appears on lines 8 and 9) read the entire file into memory and then close it. The next-to-last statement searches the HTML for all occurrences of ##date1## and replaces them with a formatted version of the current date. This presumes, of course, what when you created the sample.htm file, you inserted the string ##date1## wherever you wanted the date to appear. You can perform this trick as often as you want; for each variable item, just add a unique string to the HTML and a new Replace statement to your code. The most common problem with the CDONTS.NewMail object is that the Send method completes normally but the message never arrives. Here are the most common reasons for this behavior:

  • The SMTP service on the Web server isn't running. If the Internet Information Services administrative tool shows that the service is stopped, start it.
  • The SMTP service isn't properly resolving the domain name portion of the recipient's e-mail address. An administrator probably needs to correct the server's DNS configuration.
  • The SMTP service on the Web server is sending mail to another server that rejects it. You will need to pursue this problem with your organization's e-mail administrator or with your service provider.
On the Web    For more information about using CDO, browse msdn.microsoft.com and search for CDONTS.NewMail.

Sending Mail with ASP.NET

If you're developing Web pages in ASP.NET, you're probably avoiding ActiveX object like CDONTS.NewMail. Fortunately, the Microsoft .NET Framework includes a System.Web.Mail namespace that can send mail just as easily. Here's some sample code:

Dim msgMail as New MailMessage
msgMail.From = "hannah@deadletter.msn.com"
msgMail.To = "herman@deadletter.msn.com"
msgMail.Subject = "Happy Birthday"
msgMail.Body = "If your birthday is today, have a good one."
SmtpMail.SmtpServer = "smtp.deadletter.msn.com"
SmtpMail.Send(msgMail)

If this code reminds you of the example in the previous section, there's a reason: The System.Web.Mail namespace is basically a safe front end to CDO. Notice, however, the next to last statement, which specifies an SMTP server to use. Specifying a valid SMTP server frees you from many of the forwarding and relay problems that occur when classic ASP pages send mail through the Microsoft SMTP server on the Web server.

In the Insider Extra files, the email folder contains an ASP.NET page named mailer.aspx that uses this technique to send mail. Be sure to change the From e-mail address and the SmtpServer property before attempting to use this page on your Web server.

To attach a file to an e-mail message you create in ASP.NET, use a statement such as the following:

msgMail.Attachments.Add("C:\Temp\yourpic.jpg")

If you know only the URL—and not the physical file location—of the file you want to attach, run the URL through the Server.MapPath method. Here's an example:

msgMail.Attachments.Add(Server.MapPath("..\images\yourpic.jpg"))

To format a message as HTML, first use a statement like this to modify the BodyFormat property:

msgMail.BodyFormat = Mail.MailFormat.HTML

Tip   The opposite of Mail.MailFormat.HTML is the default, Mail.MailFormat.Text.

Then store HTML code in the Body property, like this:

msgMail.Body = "<html><body>" & _
    "<p>Have a <b>very</b> happy birthday</p>" & _
    "</body></html>"

The following code reads HTML from a file and stores it as the body of a message. This code avoids the Scripting.FileSystem object, which is an ActiveX control, opting instead to use the Stream and StreamReader classes from the System.IO namespace. The Stream class retrieves the physical file bits, and the StreamReader class provides convenient methods like the ReadToEnd method in this example.

Dim stmHtml As Stream
Dim srdHtml As StreamReader
Dim strHtml As String
stmHtml = File.OpenRead(Server.MapPath("sample.htm"))
srdHtml = New StreamReader(stmHtml, System.Text.Encoding.ASCII)
strHtml = srdHtml.ReadToEnd()
srdHtml.Close()
strHtml = Replace(strHtml, "##date1##", Format(Now, " Long Date"))
msgMail.Body = strHtml

In practice, this block of code would appear in place of the following statement from the sample earlier in this section.

msgMail.Body = "If your birthday is today, have a good one."

On the Web   For more information about sending mail from an ASP.NET page, browse msdn.microsoft.com and search for System.Web.Mail.
Show: