Sending E-mail from Your MultiValue Programs - Part 2 - Using IIS
Last issue I talked about how to create a simple e-mail, with headers and body and all. In this article I'll start talking about how to take the e-mail you formatted and get it sent from your system.
You have several different options to consider, depending on which host system your MultiValue database is on.
- IIS (Windows)
- SendMail (Linux/AIX/*nix)
- Blat (Windows)
- Exchange Email Server (Windows)
- Native SMTP client
There are many other options depending on the SMTP server you use in-house. Some of these have easy e-mail sending interfaces. Others require some scripting, but all are available to you.
While sending e-mail using an SMTP server is easy, getting your SMTP server setup and accessing the Internet correctly is a much larger topic. When setting up an SMTP server or client, you need to consider all the security features the SMTP server may be implementing.
Spam filters, Relay authentication, Domain Keys, and many other issues can cause your e-mails to be rejected by the server. This may look to your users or systems admins as something being wrong with your e-mail sending program, when in reality it is outside of your control.
An example of this is when your company e-mail admins implement "Relay Authentication." This requires you to provide username/password along with your e-mail message to your sending program. This can be problematic if you are using TLS encryption for relaying.
While these are important issues, they are outside of what we are going to cover in these articles.
Since we have to start somewhere, we will start with IIS (Internet Information Server).
Most of us have a Windows XP, 2000, 2003, or 2008 server on site. This means we have an IIS server available to us to send e-mail with. IIS includes a SMTP service that you can integrate into your database process without any extra third-party tools or command-line tools.
The only requirement is getting the e-mail written to the Windows machine that you have the SMTP Service installed on. Writing these files to the windows machine will vary depending on the type of database you have. If you do not know which MultiValue Basic functions to use, look for OSWRITE, WRITESEQ, or Q pointers that allows you to write to a specific directory location.
If you are on a Linux, AIX, or *nix system, you'll likely need to setup SAMBA or FTP to be able to write to a specific Windows directory.
(See the web site to find examples of writing text and sequential files to the O/S.)
Getting SMTP Up and Running
If you haven't used the SMTP service before, it must be running on your IIS server. The SMTP service is part of the default IIS installation. If you performed a custom IIS installation and chose not to install the service, you can easily add it from Control Panel Add/Remove Programs. To do so, open Add/Remove Programs, click Add/Remove Windows Components, select the Internet Information Services check box, and 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.
Smart Host 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 e-mail 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 e-mail 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 e-mail 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 e-mail messages to that SMTP server for relay to the Internet. To set up a relay to a smart host on an IIS machine, follow these steps:
Open ISM, right-click the SMTP server, then select Properties.
Click the Delivery tab, and 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 e-mail message look as though it came from a point other than its true origin. You can easily fake return addresses on e-mail messages. For example, if I enter "MyDomain.com" in the Masquerade domain field, mail messages would appear to come from addresses such as nathan@MyDomain.com .
Now comes the easy part. All you need to do is write your e-mail item to the following directory with a unique file name and the extension of ".eml".
As easy as that sounds, there is one catch. If you write directly to the folder, and the e-mail is large, you may find the SMTP server trying to process the e-mail before you are ready. So instead of writing the e-mail directly to that folder, write it to a temp folder. You can then move the e-mail from the
\tempmail folder to the \pickup folder. You can do this easily with the following command-line process:
move c:\inetpub\mailroot\pickup\tempmail\testemail.eml c:\inetpub\mailroot\pickup\ /Y
Again, doing this can be easy or hard depending on if you are on the same machine as the SMTP service. The Windows-based MultiValue systems have it easy in this regard, since you can just use the EXECUTE statement to process a native Windows command.
Take a look at figure 2 for an example program of how I've done this. The program was written in D3/NT, so you will have to look at your MultiValue system's documentation to see how to do this using other systems.
SUBROUTINE SEND.EMAIL(TO.ADDR,FROM.ADDR,SUBJECT,MSG) EQU AM TO CHAR(254), VM TO CHAR(253), SVM TO CHAR(252) * OPEN "EMAIL.PICKUP" TO EMAIL.PICKUP.FILE ELSE *** Can not open file, then create the file pointer *** in Master Dictionary. * Q.ITEM = "Q" Q.ITEM<3> = "DOS:c:\inetpub\mailroot\pickup\tempmail\" OPEN "MD" TO MD.FILE ELSE STOP 201, "MD" WRITE Q.ITEM ON MD.FILE, "EMAIL.PICKUP" * *** Try opening the file again. * OPEN "EMAIL.PICKUP" TO EMAIL.PICKUP.FILE ELSE STOP 201, "EMAIL.PICKUP" END * EMAIL.DATE = OCONV(DATE(),"DWA")[1,3] EMAIL.DATE = EMAIL.DATE :", ": OCONV(DATE(),"DD") EMAIL.DATE = EMAIL.DATE :" ": OCONV(DATE(),"DMA")[1,3] EMAIL.DATE = EMAIL.DATE :" ": OCONV(DATE(),"DY") EMAIL.DATE = EMAIL.DATE :" ": OCONV(TIME(),"MT") EMAIL.DATE = EMAIL.DATE :" MST" * EMAIL.ITEM = "" EMAIL.ITEM<1> = "To: ": TO.ADDR EMAIL.ITEM<2> = "From: ": FROM.ADDR EMAIL.ITEM<3> = "Subject: ": SUBJECT EMAIL.ITEM<4> = "Date: ": EMAIL.DATE EMAIL.ITEM<5> = "MIME-Version: 1.0" EMAIL.ITEM<6> = "Context-Type: text/plain" EMAIL.ITEM<7> = "Context-Transfer-Encoding: 7bit" EMAIL.ITEM<8> = "" EMAIL.ITEM<9> = MSG * EMAIL.ID = DATE():TIME():".eml" WRITE EMAIL.ITEM ON EMAIL.PICKUP.FILE, EMAIL.ID * MOVE.CMD "move c:\inetpub\mailroot\pickup\tempmail\": EMAIL.ID :" " MOVE.CMD = MOVE.CMD :" c:\inetpub\mailroot\pickup\ /Y " EXECUTE \!\: MOVE.CMD RETURN END
The key things you will need to replace are how the directory file pointer works, and how to execute a Windows command.
Drawbacks to IIS
The main drawback to using IIS is that you have to have access to the Windows machine to be able to write its directories. This may create a security concern in your enterprise.
The other issue with using IIS is that once it's running, anything that gets dropped into that folder will be sent as an e-mail, and you won't have any control over it. There is no logon process to verify you can send the e-mail through your servers, so if a malware program or user find out that this server exists, then you have a very large security concern.
The next article I will talk about how to send an e-mail using Linux SENDMAIL.