Send email as scheduled task using vbscript on Windows XP

Posted on 2005-04-26
Last Modified: 2010-05-18
I have a VBscript that will enumerate the file name in a specific folder, set it as a variable, then send it as an attachment in an email.  

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService. _
    ExecQuery("Select * from CIM_DataFile where Path = '\\syncout\\'")

For Each objFile in colFiles
  attachFile = (

Set objEmail = CreateObject("CDO.Message")

' Assign message properties (To, From, Subject, etc.)
objEmail.From = ""
objEmail.To = "testuser@cpaw"
objEmail.Subject = "Sync email file"
objEmail.Textbody = "Sync files attached"
objEmail.Configuration.Fields.Item ("") = _

objEmail.AddAttachment AttachFile

' Assign additional message properties, update the object, and send the message
objEmail.Configuration.Fields.Item ("") = 2
objEmail.Configuration.Fields.Item ("") = 25

This needs to be done twice per day.  I'm currently running it as a scheduled task on a Windows XP (SP1) box.  It works fine as long as the user (Local admin) account is logged in.  Once logged off the scheduled task fails and the following error is recorded in the event log:

Event Type: Error
Event Source: DCOM
Event Category: None
Event ID: 10002
Date:  4/22/2005
Time:  3:45:02 PM
Computer: CHV0DTXP0011
Access denied attempting to launch a DCOM Server. The server is:
The user is SYSTEM/NT AUTHORITY, SID=S-1-5-18.

The server 05D1D5D8-18D1-4B83-85ED-A0F99D53C885} shows in the registry as CCM WMI Event Provider.  I imagine it's a permissions issue with the WMI service or DCOM. I've tried changing some permissions for both but nothing has worked so far. Ideas?
Question by:mmchale
    LVL 7

    Expert Comment

    This sounds like a DCOM permissions issue for sure.  Who is the scheduled task running as?  Since it shows System, it makes me think that you may have used at.exe to schedule this?  It may be easier to just ensure that the scheduled job is running as the local admin.  Good luck!
    LVL 32

    Accepted Solution

    I think the CDO is MAPI based which generally needs a loged in user. At least when i tried it did.

    I gave up and used an SMTP mail method.   using MailSender Dll  
    [[ not sure it handles attachments in free version ]]

    '====REQUIRES ---
    ' Free ASPEmail 4.2

            Dim oEmail As MailSender
            Set oEmail = New MailSender    'create email object
            errptr = 2
            Dim testvar As Variant
            evtcount = 0: evtoverdue = 0
            Do While Not ibset.EOF

              'check for reminders on for relevant staff
                Dim msg As String, msg_title As String
                'compose message
                msg_title = "Info Event Reminder : " & ibset.Fields("etd")
                MsgAdd msg, "Event type : " & ibset.Fields("etd")
                'add all the info to the message
                With oEmail
                                        errptr = 5

                  .Host = ""
                  .From = ""
                  .FromName = "InfoBase"
                  .IsHTML = False
                  .Priority = msgpriority
                  'set the subject
                  .Subject = msg_title

                  mailrecip = ibset.Fields("emailint")

                  .AddAddress mailrecip, tmp  '"Info user"
                  'set the text
                  .Body = msg
                    'here's where we'd actually send it
                End With


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now