使用VBS调用文本文件以填充到电子邮件中的字段

时间:2014-06-26 17:12:55

标签: email vbscript

我正在创建一个放置在服务器上的脚本,无需用户交互即可在满足特定条件时发送电子邮件。我已经运行了条件脚本,但我想让脚本易于部署和修改每个服务器。

我正在尝试让脚本调用文本文件来填充电子邮件中的TO字段。文本文件将每行放置一个电子邮件地址(我在地址末尾加了一个分号,因为我知道多个TO地址用分号分隔)

我在调用脚本时尝试了许多不同的变体,我在To行上遇到错误,或者它通过脚本完成,我得到一个没有定义收件人的错误。以下是以下脚本:

`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@server.com"
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 个答案:

答案 0 :(得分:1)

To对象的objEmail字段应该是包含一个或多个电子邮件地址的字符串。您正在为其分配TextStream对象(objFileEmailAddresses)。

您说您的电子邮件地址已经以分号结尾了吗?试试这个:

' 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

你可以摆脱Dictionary对象。除非您担心您可能会多次列出相同的电子邮件地址,否则我们可能需要使用一个。