Use the SMTP service to send email messages from your Web server
Despite the explosion of Web sites available on the Internet, email is still the most widely used Internet service. You can easily configure your IIS machines to send email messages over the Internet or through intranets. To do so, you need the Microsoft SMTP Service. I show you how to configure the SMTP service, includ-ing some advanced parameters, and provide a few downloadable code samples that you can leverage in your environments.
Getting SMTP Up and Running
If you haven't used the SMTP service before, it must be running on your Web server. The SMTP service is part of the default IIS installation. If you performed a custom IIS 5.0 or IIS 4.0 installation and chose not to install the service, you can easily add it from the Control Panel Add/Remove Programs applet. To do so, open the Add/Remove Programs applet, click Add/Remove Windows Components, select the Internet Information Services check box, then click Details.
In a default IIS installation, one SMTP server is installed. You can verify that it's running by opening Internet Services Manager (ISM) and viewing the SMTP server. FTP sites appear first, followed by Web sites and SMTP sites. If the SMTP server isn't running, right-click it, then select Start.
Advanced Configuration
Now, let's look at a few of the advanced configuration options. Most large or enterprise companies place SMTP servers behind firewalls that block any direct outbound SMTP traffic through port 25. If such is the case in your shop, your email messages will end up in the Badmail folder with cryptic router error-message files. If a smart host is available on your network, you can use it to relay all SMTP messages to the Internet.
In most cases, a smart host is simply another SMTP server with permission to relay outgoing email messages from other internal SMTP servers directly to the Internet. Thus, a smart host should be able to connect simultaneously to both the internal network and the Internet to work as the email gateway. If this setup exists in your company, you can simply add the IP address of the smart host on your network and IIS will send its email messages to that SMTP server for relay to the Internet. To set up a relay to a smart host on an IIS 5.0 or IIS 4.0 machine, follow these steps:
- Open ISM, right-click the SMTP server, then select Properties.
- Click the Delivery tab, then click Advanced to open the Advanced Delivery dialog box, which Figure 1 shows.
- In the Smart host field, type either the smart host's Fully Qualified Domain Name (FQDN) or its IP address surrounded by square brackets (e.g., [10.1.4.25]). The brackets help speed up the DNS lookup process by specifically telling the SMTP server that the numbers are an IP address. Click OK twice to complete the changes.
In the Advanced Delivery dialog box, notice the Masquerade domain field. The masquerade domain makes an email message look as though it came from a point other than its true origin. You can easily fake return addresses on email messages. For example, if I enter
HuckabyIISParadise.com
in the Masquerade domain field, mail messages would appear to come from addresses such as tim@huckabyiis paradise.com. Obviously, the practical joke potential is spectacular here, but be forewarned: Configuring POP3 mail servers to check the domain before accepting an email message is equally easy.
Sending Mail Simply with CDONTS
With your SMTP server configured, you're ready for a simple test using Collaboration Data Objects for Windows NT Server. CDONTS is a set of COM components that's installed with IIS and exposes messaging objects to languages such as VBScript in Active Server Pages (ASP) applications. CDONTS has a simple object called NewMail that lets you send email messages from IIS with only a few lines of code. (I show you the inner workings of the CDONTS NewMail object in the next section.) In the simplest implementation of this object, you need only the few lines of code that Listing 1 shows.
Create a sample folder in your test IIS Web server's file system called \inetpub\wwwroot\smtp. Then, download smtp-cdonts.asp from the Code Library on the Windows Web Solutions Web site (http://www.windowswebsolutions.com) and place the file in the sample folder. Run your favorite editor (e.g., Microsoft Visual InterDev, Notepad) and edit the From, To, Subject, and Message Content parameters appropriately. (You'll probably want to send messages to yourself while testing.) Save the file. Next, open a browser and navigate to smtp-cdonts.asp. IIS will send an email message from your SMTP server.
To properly deliver email messages, you need Internet connectivity from your server. If you don't have Internet access, verifying that the SMTP server is sending email messages is easy because the email messages' file images are dropped into the queue in \inetpub\mailroot\queue. You can use any email client (e.g., Microsoft Outlook Express) to view these files. If anything is wrong in a message's addressing, the SMTP server moves the email message to \inetpub\mailroot\badmail.
tauseef December 22, 2003