0

I am creating a script to be placed on servers to run without user interaction that will send an email when certain criteria are met. I have the criteria script running, but I want to make the script easy to deploy and modify for each individual server.

I am trying to make the script make a call to a text file to populate the TO field in the email. The text file will have the email addresses placed one per line (I was putting a semicolon on the end of the addresses since I know multiple TO addresses are separated by semicolons)

I have tried a number of different variations on calling the script and I either get an error on the To line, or it makes it through the script and I get an error that no recipients were defined. Here is the script below:

`Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFileEmailAddresses = objFSO.OpenTextFile("EmailAddresses.txt", 1)
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set wshShell = WScript.CreateObject("WScript.Shell")


strComputerName = wshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")

ServicesArray = Split (objFileEmailAddresses.ReadAll, vbNewLine)

For Each strService In ServicesArray
    objDictionary.Add strService, strService
Next



Set objEmail = CreateObject("CDO.Message")
objEmail.From = "[email protected]"
objEmail.To = objFileEmailAddresses
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp address"
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
objEmail.Configuration.Fields.Update
objEmail.Fields.Item("urn:schemas:mailheader:X-MSMail-Priority") = "High"
objEmail.Fields.Item("urn:schemas:mailheader:X-Priority") = 2
objEmail.Fields.Item("urn:schemas:httpmail:importance") = 2
objEmail.Fields.Update
objEmail.Subject = "Primary Server " & Ucase(strComputerName) & " is Rebooting Now"
objEmail.TextBody ="The primary server " & Ucase(strComputerName) & " is scheduled to reboot at this time.  The server will be offline in less than one minute.  .... "
objEmail.Send

objFileEmailAddresses.Close()`

1 Answer 1

1

The To field of your objEmail object should be a string containing one or more e-mail addresses. You're assigning a TextStream object (objFileEmailAddresses) to it.

You said your e-mail addresses already end with a semicolon? Try this instead:

' Open the text file containing all of the e-mail addresses...
Set objFileEmailAddresses = objFSO.OpenTextFile("EmailAddresses.txt", 1)

' Read the entire file. Replace newlines with nothing to get a single
' string of semicolon-separated e-mail addresses...
strAddresses = Replace(objFileEmailAddresses.ReadAll, vbCrLf, "")

' Assign the string to the e-mail object...
objEmail.To = strAddresses

You can get rid of the Dictionary object. Unless you're afraid you may have the same e-mail addresses listed more than once, in which case we may need to use one.

Sign up to request clarification or add additional context in comments.

1 Comment

No problem. Glad to hear it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.