Date created:20-aug-97

Sending mail with MAPI

I had much requests after one of my postings in the ASP mailing list, that I decided to put the sample on my web. From technology view of point, it is already outdatet. But it looks like not each one goes with MS's speed and need some older stuff.

Sending mail with MAPI is easy under some conditions. If you have MS Exchange Server installed, you can use the CDO (Collaboration Data Objects) or the toolkit coming with V5.5 If only MS Mail postoffices (MS Mail V3.2 to V3.5) are installed, one can use the predecessor of CDO, the Active Messaging (V1.1). Additionally, one need a MAPI aware product like the "full" MS Exchange Client or the newer MS Outlook CLient. "Full" means, you cannot use the client which is delivered with the OS itself.

Naturally, that's really silly to install such software at a server machine and I think this is only for MS licensing reasons (the client delivered with the OS has not the same level of interaction with the Active Messaging Library).

I had no try to use the new objects coming with Windows NT Option Pack2. Normally, they should give the simplest solution to send mail from ASP pages. But they allow only to send mail, there is no access to the underlying storage system (folders) and MAPI cannot be used as protocol. So they are out of focus if you need reading mail and doing some manipulation of folders and contents or something else.

How it works

The example is used to allow a user sending a message via mail, in my case, to a "webmaster".

    The example contains three files:
  1. mbOLEMsgDemo3DataInput.htm
    that's a HTML form to allow a user to enter the mail message text and optional his e-mail address (used only to put it in the mail message body as information for the "webmaster"). This form calls then
  2. SubmitMail3.asp
    which processes the two form fields. They are written to a (temorary) file. Than parameters for the third file are build and that file is sent to the Windows NT scheduler service. Therefor it makes use of the Shell.Run method of the "Windows Scripting Host" object. You can install WSH (it's free with the most worse docs I have ever seen from MS [they are always poor]) or you could use another component, like Stephen Genua's. The ASP script make use of the properties in the Shell object of WSH to get the temp-path and from the advanced properties of the scripting object, what is delivered with WSH (VBS shows you V3 !!). There are lots of new properties and methods, than in the VBS V2.0, which comes with ASP. These methods are used to build a temporary filename for the mail file. You could use date and time or whatever to build a workaround for a temporary filename.
  3. mbFileMail2.vbs
    That's the scheduled vbs procedure.
      It has three input parameters:
    1. Mailfilename
    2. Subject
    3. optional /nodelete qualifier
    This file (c) accesses a MS Exchange Client profile wich must be accessible to the user, which runs the schedule service. Log on as that user and define the profile. The example uses the personal distribution list "SENL" (Sent Event Notification List) to define the addressee. So to change it, log on as the scheduler service user, run the client and add/remove other mail addresses (You could use the same address book and personal folder file if you create a second profile for your ordinary login; Some easier to administer). Naturally, you can change the "SENL" to your preferred address in the code.

There are things to consider: I have modified the registry settings for WSH in that manner, that I can type "myscript" instead of "CSCRIPT myscript.VBS". This must even be true for the scheduler service user and my sample! You can schedule with cscript too. There are a lot of restrictions with the Windows NT scheduler service. You can not have console output. Writing to STDERR or STDOUT is useless. In mbFileMail2.vbs all messages are written with the WSCRIPT.ECHO method which do'nt go out in the batch. I am working an detecting running with the scheduler and sending all output to a logfile instead of WSCRIPT.ECHO. Therefore I have centralized all output in a "PutCon" subroutine, but thats not ready. You could decide to schedule a cmd procedure from ASP which runs the VBS script via calling cscript.exe. With this, you can redirect output. Redirecting with the "PutCon" routine is the easiest way.
The command line length for the scheduler command "AT" cannot exceed 259 characters. So, if you have long subjects and a long path, you'll get a problem. Use a short path for the location of the files and be aware to always define to full path to the file. If you cannot fullfill this restrictions, you should write a textfile from asp and read it in in the sending wsh script.

Some data about my environment at creation date:

 I use
-	Windows NT V4, Service Pack 3, admin hotfix,
-	ASP V3.0
-	OLE Messaging V1.1a
-	Windows Scripting Host V1.0
-	VB Script V2.0

My domain name is "MBG", my servername is "M1". My profilename for sending in the example is "IUSR_M1". So enough help, I hope you have all what you need and it useful for you!

    If you are further interested, download this files:
  1. mbOLEMsgDemo3DataInput.htm
  2. SubmitMail3.asp
  3. mbFileMail2.vbs

Remark:
All files are with extension ".txt" for some reasons. So, if you just click the link, you'll get them in your browser. Alternately, you could use the mous's right button to get the "save target as..." and a "sorry" for the whole german comments in the files.....