none
Email using Outlook from VBscript does not run in Task Scheduler RRS feed

  • Question

  • Tech people,

    I have been working on resolving an issue that involves generating an email from database information and sending through outlook.  I have got code working perfectly, so long as I run myself.  When I try to run as a Task, the email does not get sent.  It does not error, but it stops processing at the command, email.send.  The code is below.  Is this a limitation of Windows?  Or is it that I am not technically logged in as myself when the job is run, therefore it can't send?  I am not getting any errors, so it is making it a bit difficult in getting to a solution.

    Thanks a bunch!

    David Langschied

    'Define Constants
    Const ForReading = 1
    Const ForWriting = 2
    Const olFolderInbox = 6

    'Set variable values
    strComputer = "."
    MFolder = "C:\v-email\remit\"
    AFolder = "C:\v-email\remit\archive\"

    'Generate Outlook objects
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery _
        ("Select * From Win32_Process Where Name = 'outlook.exe'")
    Set oFSOInput = CreateObject("Scripting.FileSystemObject")

    'If outlook is not running, open it
    If colItems.Count = 0 Then
        Set objOutlook = CreateObject("Outlook.Application")
        Set objNamespace = objOutlook.GetNamespace("MAPI")
        objNamespace.Logon "Default Outlook Profile",, False, True    
        Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)
        objFolder.Display
    End If

    'Move through the Mail folder looking for .mail files, generate email, send, and move file to archive
    For Each oInputFile In oFSOInput.GetFolder(MFolder).Files
        oFileParts = split(oInputFile.Name,".") 
        oInputFileExt = oFileParts(1)
        oInputFileName = oFileParts(0)
        If LCase(oInputFileExt) = "mail" Then
    'Process mail file
    ProcessFiles oFSOInput, oInputFile
    'Move mail file to archive
    oFSOInput.MoveFile oInputFile,AFolder 
        End if
    Next

    'Clear File object
    Set oFSOInput = Nothing

    Sub ProcessFiles(FSOInput, InputFile)
        'Set Input Object
        Set InputFile2 = FSOInput.OpenTextFile(InputFile.path, ForReading)
        'Loop through file line by line
        Do Until InputFile2.AtEndOfStream
            strLine = InputFile2.ReadLine
            strLineNo = strLineNo + 1
            'First time through, generate HTML for email body
            If strLineNo = 1 then
       oMailBody = "<!DOCTYPE HTML PUBLIC ""-//W3C//DTD W3 HTML//EN"">"
         oMailBody = oMailBody & "<HTML>" & vbcrlf
         oMailBody = oMailBody & "<HEAD><TITLE>No Invoices</TITLE></HEAD>"
         oMailBody = oMailBody & "<BODY>" & vbcrlf
    End If
            'First four lines of file contain the from, to, cc, and subject
            If strLineNo <= 4 then
                If Left(strLine, 5) = "FROM:" then
                    oMailfrom = Mid(strLine,6,52)
                End If
                If Left(strLine, 3) = "CC:" then
                    oMailcc = Mid(strLine,4,52)
                End If
                If Left(strLine, 3) = "TO:" then
                    oMailto = Mid(strLine,4,52)
                End If
                If Left(strLine, 8) = "SUBJECT:" then
                    oMailsubj = Mid(strLine,9,80)
                End If
            Else
       'After line four, all the rest is text for the email body
       oMailbody = oMailbody & "<br>" & strLine & "</br>"
            End If
        Loop
        'Close off teh HTML of the body
        oMailBody = oMailBody & "</BODY></HTML>" 
        'Close all and clean up
        InputFile2.close
        set InputFile2 = Nothing
        'With all info, now create email
        SendMessage oMailfrom, oMailcc, oMailto, oMailsubj,  oMailbody
    end sub

    Sub SendMessage(Mailfrom, Mailcc, Mailto, Mailsubj, Mailbody)
        'Set email objects
        Set oolApp = CreateObject("Outlook.Application")
        Set email = oolApp.CreateItem(0)
        'Set email components
        email.Recipients.Add(Mailto)
        email.Subject = Mailsubj
        email.HTMLBody = MailBody
        'Send email
        email.Send
    End Sub


    Tuesday, January 12, 2016 12:56 PM

Answers

  • Hi,

    I've test  your vbs and it work fine with Task Scheduler.
    But you need to put a real user, not a system account in "When running the task, use the following user account:".

    you can probably use CDO to send your Email throug SMTP server. Take a look at 
    https://support.microsoft.com/en-us/kb/286431

    you need to change all parts "<> " and specially with a valid SMTP SERVER.

    "<remote SMTP server>" 

    If you need to saved the Email, add your email address in cci


    Have a nice day. Oliv-

    • Marked as answer by David_JunFeng Thursday, January 21, 2016 1:47 AM
    Thursday, January 14, 2016 11:11 AM
  • > I have got code working perfectly, so long as I run myself.  When I try to run as a Task, the email does not get sent.  It does not error, but it stops processing at the command, email.send.

    Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

    If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.

    Instead, you may consider using a low-level API on which Outlook is based on - Extended MAPI or any other wrappers around that API. 

    • Marked as answer by David_JunFeng Thursday, January 21, 2016 1:48 AM
    Tuesday, January 12, 2016 10:07 PM
  • You might be able to accomplish your objective by using the Powershell to send your email from the task scheduler instead of Outlook.  For a sample script see https://gallery.technet.microsoft.com/scriptcenter/Send-HTML-Email-Powershell-6653235c
    • Marked as answer by David_JunFeng Thursday, January 21, 2016 1:48 AM
    Wednesday, January 13, 2016 2:53 PM

All replies

  • Did you set up the task to run under your own ID?

    It's also possible that you are bumping up against Outlook's built-in security measures.

    • Edited by RLWA32 Tuesday, January 12, 2016 3:41 PM
    Tuesday, January 12, 2016 2:58 PM
  • > I have got code working perfectly, so long as I run myself.  When I try to run as a Task, the email does not get sent.  It does not error, but it stops processing at the command, email.send.

    Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

    If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.

    Instead, you may consider using a low-level API on which Outlook is based on - Extended MAPI or any other wrappers around that API. 

    • Marked as answer by David_JunFeng Thursday, January 21, 2016 1:48 AM
    Tuesday, January 12, 2016 10:07 PM
  • You might be able to accomplish your objective by using the Powershell to send your email from the task scheduler instead of Outlook.  For a sample script see https://gallery.technet.microsoft.com/scriptcenter/Send-HTML-Email-Powershell-6653235c
    • Marked as answer by David_JunFeng Thursday, January 21, 2016 1:48 AM
    Wednesday, January 13, 2016 2:53 PM
  • Hi,

    I've test  your vbs and it work fine with Task Scheduler.
    But you need to put a real user, not a system account in "When running the task, use the following user account:".

    you can probably use CDO to send your Email throug SMTP server. Take a look at 
    https://support.microsoft.com/en-us/kb/286431

    you need to change all parts "<> " and specially with a valid SMTP SERVER.

    "<remote SMTP server>" 

    If you need to saved the Email, add your email address in cci


    Have a nice day. Oliv-

    • Marked as answer by David_JunFeng Thursday, January 21, 2016 1:47 AM
    Thursday, January 14, 2016 11:11 AM
  • The issue was that at some point I had been told that I should run with highest privileges.  I unchecked vis-a-vis your screen shot and it works just as desired thanks!
    Wednesday, January 27, 2016 7:57 PM