May 1999Sending Email via ASP and CDO for NT Serverby Scot JohnsonEmail has become an essential communication tool in today's information-centric world. With an email client on virtually every desktop, this electronic notification mechanism is extending beyond person-to-person messaging capabilities and is playing a critical role in application notification, broadcast, and alert systems. Stand-alone processes and applications use email to automatically notify individuals, such as network administrators, backup operators, or system attendants, that disk space is getting low, broadcast status updates, or a process is complete. This article discusses how to use Internet mail capabilities and Collaboration Data Object (CDO) for NT Server to provide real-time messaging capabilities. After reading this article, you won't be a messaging expert, but you will be able to work with your messaging specialist to add email capabilities to your applications. Let's get started! Covering the basics of messaging and CDO In order to facilitate sending Internet email from the Internet Information Server (IIS), Microsoft created CDO to act as an interface into a messaging service based on standards set forth in Microsoft's Messaging Application Program Interfaces (MAPI). MAPI is a messaging architecture created to standardize messaging communications across different hardware and software platforms. In addition, MAPI provides a transport layer into the final messaging service that's ultimately responsible for housing and/or delivering the message. Therefore, by creating a simpler interface layer, CDO makes developing email-enabled applications easier by reducing the complexity of having to code directly to MAPI-compliant services. One reason the CDO is powerful is because it exposes the groupware functionality and message stores found in the Microsoft Exchange Server and Microsoft Outlook. These services allow you to send and receive Internet email, access calendar and scheduling information, and access public folders and threaded discussion databases. You may have already used CDO without knowing it. If you've used the Outlook Web Access (OWA) for Microsoft Exchange 5.5, then you've used CDO. In a technology evolution, there are different versions of CDO, each with its own capabilities. CDO was initially released as OLE Messaging (OLEMSG.DLL) with Exchange 4.0. With the release of Exchange 5.0, Active Messaging was upgraded to CDO version 1.1 and then upgraded to version 1.2, with the rollout of Exchange 5.5. CDO 1.2 actually consists of two libraries: the CDO Library (CDO.DLL) and the CDO Rendering Library (CDOHTML.DLL). The CDO Library enabled a connection into a MAPI mailbox and the CDO Rendering Object allowed content of Exchange folder to be rendered in HTML format. Just when you thought it was safe, CDO for NTS (CDONTS.DLL) was released with IIS 4.0. CDONTS is a scaled back version of the full-blown CDO library--everything but the Exchange Server specific functional, such as calendaring and scheduling. CDO for NTS is designed primarily to quickly enable Internet mail capabilities via the SMTP Server and IIS4, enabling anonymous messaging without SMTP login. SMTP server configuration In order to set up our example, we need to ensure the SMTP server is configured properly to reach your SMTP server. Let's review the steps needed in our SMTP process flow:
In order to route your message to a remote SMTP server, you must configure your
SmartHost and Fully Qualified Domain Name in the Delivery tab of the
local SMTP server. The SmartHost property tells what server to route your
messages to. The SmartHost property can be either a string domain name or an IP
address. The Fully Qualified Domain Name (FQDN) allows you to specify the
host.domain name for the SMTP server. Without changing this string, the
SMTP Server will use the name specified in DNS settings in the TCP/IP property
sheet. In this example, our FQDN is mail.northamerica.i3solutions01 and our
SmartHost is pointed to smtpmail.northamerica.i3solutions.com, as seen
Figure A: Use the SmartHost to route SMTP requests to an additional SMTP Server.
Now that we're properly sending SMTP messages, there's only one more catch, depending on the location installation of the IIS and SMTP servers. If the SMTP Server and IIS Server are installed on the same machine with the same IP address, the local SMTP service will automatically send the outbound email without having to set any special user permissions. If your SMTP Server is located on a separate machine from your IIS machine, you have to give the Web server operator's Internet anonymous user account rights to the remote SMTP server. Do this by selecting this account under the Operator tab in the Local SMTP Site properties sheet, as seen in Figure B. Figure B: Assign operator permissions for a remote SMTP Server.
File summary Once your system is properly configured, we're going to use two sets of pages that will demonstrate how to use the CDONTS object. These pages will demonstrate how to send a message, attach documents, control formatting and encoding properties, enable HTML within message body, and add custom message headers. In this article, we'll use files with the .htm extensions as the data input pages, and pages with the .asp extensions to process the CDONTS objects. Table A illustrates the page names and descriptions in this month's Zip file. Table A: File Listing
Table B: CDONTS properties and descriptions
After setting the NewMail properties, you can use the NewMail methods, shown in Table C, to perform actions on a message. These actions include adding attachments and URLs, sending messages, and setting local IDs. Table C: CDONTS method descriptions
Using basic CDONTS techniques Consider the following situation: we want to automatically send a message to notify the network administrator that a specific amount of concurrent HTTP connections was reached. Granted, in most situations when you're building messaging systems that allow applications to alert or notify external processes, you won't build a user input page. However, for the sake of this demonstration, we've created a user input screen, named cdonts_simpleInput.htm. This Web page, shown in Figure C, enables the user to specify whom to send the message to, from whom the message is sent, the subject matter, and set the Importance level and enter the message text. Figure C: cdonts_simpleinput.htm as viewed in the browser.
After the message is sent, the network administrator would receive the notification in his email via Outlook 2000, as shown in Figure D. Figure D: Here's the alert message delivered by CDONTS.
Now, let's take a closer look at the individual pages that make this email processing possible. First, let's review the HTML code that's used to collect the data cdonts_simpleInput.htm. Upon review, you'll notice that this page contains HTML Form elements with FrontPage validation and posts its results to cdonts_simpleInput.asp for processing, as seen in Listing A. Listing A: cdonts_simpleInput.htm
The page cdonts_simpleInput.asp accepts the posted data and process the CDONTS
messaging request. In addition, this page displays results back to the user, as
seen in Figure E. A close review of cdonts_simpleInput.asp will expose
the core CDONTS processing power, as shown in Listing B.
Figure E: This is the output from cdonts_simpleInput.asp after Internet email is sent.
Listing B: cdonts_simpleInput.asp
If we sort through the code in Listing B, the core CDONTS processing
takes place in the following code snippet:
This code snippet uses the Request.Form object to assign variables into the To,
From, Subject, Importance, and Body of the message. Next, the CDONTS object is
instantiated with the Server.CreateObject method:
Now that an instance of the object exists and the object's properties are set,
the message is sent using the send method:
You could have also sent the message using just three lines of code:
Although the send method syntax allows all arguments to be optional, your
message isn't much good if it isn't sent to a valid SMTP address (user@domain):
Conclusion As you can see from our example, the CDONTS technology is straightforward, with one quirk to keep in mind: once the CDONTS object has sent the message, the object can't be used to send another message. If you find yourself in a situation where you have to send multiple messages within the same script, you'll need to create a process to serially creating unique CDONTS objects.
Copyright © 1999, ZD
Inc. All rights reserved. ZD Journals and the ZD Journals logo are trademarks of ZD
Inc. Reproduction in whole or in part in any form or medium without
express written permission of ZD Inc. is prohibited. All other product
names and logos are trademarks or registered trademarks of their
respective owners.
|