HOWTO: Build a VB/Messaging Application to Run from a Service

ID: Q177851

The information in this article applies to:

SUMMARY

This article provides sample code demonstrating how to create a Visual Basic based Messaging application using Collaboration Data Objects (previously named Active Messaging) that runs as a service under Windows NT version 4.0.

MORE INFORMATION

This application is dependent on the Collaboration Data Objects (CDO) library being referenced by the Visual Basic project. Since this application is being built with the intent to run as a service, it is built in a code module (not a form). In addition, you should select the Unattended Execution check box on the General tab of the Project Properties dialog box. Selecting this check box indicates that the project is intended to run without user interaction. Unattended projects have no interface elements. Any run time functions like messages that normally result in user interaction are written to an event log.

This article is divided into three primary sections:

Method 1

Paste the following code into a new BAS Module. Make sure that the Collaboration Data Objects (CDO) library is currently referenced by the project. Build the executable and follow the directions in the code comments below:

   Sub Main()
    ' How to write a Visual Basic Active Messaging App that can be run
    ' from the Windows NT Schedule Service.
    ' ----------------------------------------------------------------
    ' To demonstrate this functionality, the Schedule Service MUST be
    ' run in the context of a USER ACCOUNT (not the system account.)
    ' To set the Schedule Service Context:
    '   - From the Control Panel select the Services icon
    '   - Scroll down and single click the Schedule Service
    '   - Single click the Startup button
    '   - Select "This Account", and enter YOURDOMAIN\YOURACCOUNT
    '   - It is also suggested that Startup be Manual
    '
    ' With this done, choose the Start button (you should see a dialog box
    ' indicating success). Close the Control Panel.
    '
    ' Now you need to schedule the application to execute. To do this:
    '   - From the Start Menu choose Run
    '   - At the prompt, enter "AT HH:MM PATH\MYEXE.EXE" where
    '   * HH:MM is the time the application is to execute. This is a 24
    '   hour clock, and time is based on the system clock.
    '   * PATH\MYEXE.EXE is path and filename of your EXE.
    '
    SendScheduledMsg
   End Sub

   Sub SendScheduledMsg()
    'Setup variables for use
    Dim objSession As MAPI.Session
    Dim objMsg As Message
    Dim objRecip As Recipient

    'Create Session and Logon
    Set objSession = CreateObject("mapi.session")
    objSession.Logon _
          nomail:=True, _
          profileinfo:="YOURSERVER" & vbLf & "YOURMAILBOX"

    'Create new Message and set properties.
    Set objMsg = _
     objSession.Outbox.Messages.Add( _
       Subject:="This is a Message Sent From a Scheduled Service", _
       Text:="This is a test")

    'Add a Recipient.
     Set objRecip = objMsg.Recipients.Add(Name:="YourRecipsEmailName")
     objRecip.Resolve

    'Send the Message and Logoff.
    objMsg.Send
    objSession.Logoff

    'Cleanup
    Set objRecip = Nothing
    Set objMsg = Nothing
    Set objSession = Nothing
   End Sub

Method 2

Paste the following code into a new BAS Module. Make sure that the Collaboration Data Objects (CDO) library is currently referenced by the project. Build the executable. Follow the directions in the code comments below:

   Sub Main()
    'How to write a Visual Basic CDO Application that can be run
    'from the Windows NT Schedule Service indirectly using the SU
    'utility from the Windows NT Resource Kit.
    '----------------------------------------------------------------
    'To demonstrate this functionality, the Schedule Service can be
    'run in the context of the SYSTEM or USER ACCOUNT. However, while
    'using this option the context will most likely be system.
    'To set the Schedule Service Context:
    '   - From the Control Panel select the Services icon
    '   - Scroll down and single click the Schedule Service
    '   - Single click the Startup button
    '   - Under Log On As, select "System Account."
    '   - It is also suggested that Startup be Manual, and that
    '     service interaction with the Desktop be disallowed.
    '
    'With this done choose the Start button (you should see a dialog box
    'indicating success). Close Control Panel.
    '
    'Now you need to schedule the SU utility to execute. To do this:
    '   - From the Start Menu choose Run
    '   - At the prompt, enter "AT HH:MM PATH\MYBAT.BAT" where
    '     * HH:MM is the time the application is to execute. This is a 24
    '     hour clock, and time is based on the system clock.
    '     * PATH\MYBAT.BAT is the path and filename of a batch file that
    '     sets values of system variables needed by SU, then launches
    '     SU itself. The batch file needs to contain the following
    '     information:
    '
    '        set SU_COMMANDLINE=MyVBexe.exe
    '        set SU_DOMAIN=MyDomain
    '        set SU_PASSWORD=MyDomainPassword
    '        su.exe MyDomainAcctName
    '
    'Please note that this is only one way to start SU. The Su.txt file
    'that accompanies Su.exe documents several alternate methods of
    'launching the SU utility. Please see Su.txt for additional
    'information.
    '
    SendScheduledMsg
   End Sub

   Sub SendScheduledMsg()
    'Setup variables for use.
    Dim objSession As MAPI.Session
    Dim objMsg As Message
    Dim objRecip As Recipient

   'Create Session and Logon
    Set objSession = CreateObject("mapi.session")

   'The following Logon designates a specific ProfileName. The
   'Microsoft Knowledge Base article that describes how to search
   'the HKEY_CURENT_USER hive (which was loaded by SU) of the system
   'registry for the default profile in case a specific profile name
   'is either unknown of intentionally not designated is listed in the
   'REFERENCES section.
   '

   '
   objSession.Logon _
     profileName:="ValidProfileNameForUserAccountNamedInBatchFile", _
     nomail:=True

   'Create new Message and set properties
   Set objMsg = _
   objSession.Outbox.Messages.Add( _
     Subject:="This is a Message Sent From a Scheduled Service", _
     Text:="This is a test")

   'Add a Recipient
   Set objRecip = objMsg.Recipients.Add(Name:="YourRecipsEmailName")
   objRecip.Resolve

   'Send the Message and Logoff
   objMsg.Send
   objSession.Logoff

   'Cleanup
    Set objRecip = Nothing
    Set objMsg = Nothing
    Set objSession = Nothing
   End Sub

Method 3

How to write and register as a Service a Visual Basic Application that uses Collaboration Data Objects (CDO).

The author prefaces this section with a precautionary warning that this method is not supported and is not typically programmatically sound. This process, using MAPI functionality in the example, is documented in the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q175948
   TITLE     : INFO: Running Visual Basic Applications as Windows NT
               Services

REFERENCES

For information on where to acquire the most recent version of this library tested for client-side use, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q171440
   TITLE     : INFO: Where to Acquire the Collaboration Data Objects
                Libraries

For additional information about Collaboration Data Objects versus Active Messaging, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q176916
   TITLE     : INFO: Active Messaging and Collaboration Data Objects (CDO)

   ARTICLE-ID: Q171422
   TITLE     : Logging on to Active Messaging Session w/Default Profile

Additional query words: ActMsg Active Messaging
Keywords          : kbcode kbCDO110 kbCDO120 kbMsg kbVBp kbGrpMsg 
Version           : WINDOWS:1.1,1.2,1.21
Platform          : WINDOWS winnt
Issue type        : kbhowto

Last Reviewed: July 14, 1999