In this vbs not able to add multiple email addresses. I get unknow. if added more than 1 email address.

Hi,

In this vbs not able to add multiple email addresses. I get unknow. if added more than 1 email address.

dim ToAddress,MessageSubject,MessageBody
      ToAddress = "Sharath@lth.com ;Sharath@lth.com"
      MessageSubject = "Enum Pcs report..."

Can anyone suggest how should i enter more than 1 email address.

Regards
sharath
LVL 11
bsharathAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RimvisCommented:
Just a guess:a)try  to remove space between "com" and ";"b)use "," instead of ";"
0
bsharathAuthor Commented:
Did not work
0
Sarika30Commented:
Try this:
Recipient = "you@yours.com;me@mine.com"
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

bsharathAuthor Commented:
Still no. It does not work
0
Sarika30Commented:
Can you please post the full code you are using for sending mail?
0
bsharathAuthor Commented:
Please find the full code below
const SCHEDULER_TASK_NAME = "PcsEnumTask"
const SCHEDULER_TASK_START_TIME = "12:00:00"
const ROOT_OU = "ou=Ch,OU=I,OU=Offices,"

dim LogResult,args
Dim objShell : Set objShell = CreateObject("WScript.Shell")
Dim strOutput : strOutput = objShell.Exec("schtasks /query /fo list").StdOut.ReadAll

if InStr(data, SCHEDULER_TASK_NAME) = 0 then
        WScript.Echo SCHEDULER_TASK_NAME & " found.."
end if

if InStr(strOutput, SCHEDULER_TASK_NAME) = 0 then
'        WScript.Echo "Create task scheduler [" & SCHEDULER_TASK_NAME & "]..."
        'create task scheduler
        args = "schtasks /Create /F /SC DAILY /TN " & SCHEDULER_TASK_NAME & " /TR """ & Wscript.ScriptFullName & """ /ST " & SCHEDULER_TASK_START_TIME
		'WScript.Echo "args: " & args
		objShell.Run args, 1, True

        'WScript.Echo "Task scheduler [" & SCHEDULER_TASK_NAME & "] created successfully"
else
        'numertae pcs and email
        EnumPcs
		EnumGroups
		EnumUsers
        EmailResult
end if

Sub EmailResult
dim ToAddress,MessageSubject,MessageBody
'ToAddress = "Sharath@lth.com,Sharath@th.com"

	MessageSubject = "Enum Pcs report..."

MessageBody = LogResult 

	Set ol = WScript.CreateObject("Outlook.Application")
	Set ns = ol.getNamespace("MAPI")
	ns.logon "","",true,false
	Set newMail = ol.CreateItem(olMailItem)
	newMail.Subject = MessageSubject
	newMail.Body = MessageBody & vbCrLf

	' validate the recipient, just in case...
	Set myRecipient = ns.CreateRecipient(ToAddress)
	myRecipient.Resolve
	If Not myRecipient.Resolved Then
	   MsgBox "unknown recipient"
	Else
	   newMail.Recipients.Add(myRecipient)
	   newMail.Send
	End If

Set ol = Nothing


End Sub

Sub EnumUsers
	LogResult = LogResult & vbNewLine & "Users Report:"  & vbNewLine
	dtmDate = Now
         
        strYear = Right(Year(dtmDate), 2)
        strMonth = Month(dtmDate)
        If Len(strMonth) < 2 Then strMonth = "0" & strMonth
        strDay = Day(dtmDate)
        If Len(strDay) < 2 Then strDay = "0" & strDay
         
        strStartDate = strYear & strMonth & strDay & "000000Z"
        strEndDate = strYear & strMonth & strDay & "235959Z"
         
        strFilter = "(&(createTimeStamp>=" & strStartDate & ")(createTimeStamp<=" & strEndDate & ")(objectCategory=user))"

        Set objConnection = CreateObject("ADODB.Connection")
        objConnection.Provider = "ADsDSOObject"
        objConnection.Open "Active Directory Provider"
         
        Set objRootDSE = GetObject("LDAP://RootDSE")
        Set objRecordSet = objConnection.Execute( _
          "<LDAP://" & ROOT_OU & objRootDSE.Get("defaultNamingContext") & ">;" & _
          strFilter & ";name,createTimeStamp;subtree")
        Set objRootDSE = Nothing
         
        While Not objRecordSet.EOF
          dtmCreateTimeStamp = CDate(objRecordSet.Fields("createTimeStamp").Value)
          strMessage = objRecordSet.Fields("name") & " " & _
                objRecordSet.Fields("createTimeStamp")
         
          LogResult = LogResult & strMessage & vbcrlf 
          objRecordSet.MoveNext
        WEnd
end sub

Sub EnumGroups
LogResult = LogResult & vbNewLine & "Groups Report:"  & vbNewLine

	dtmDate = Now
         
        strYear = Right(Year(dtmDate), 2)
        strMonth = Month(dtmDate)
        If Len(strMonth) < 2 Then strMonth = "0" & strMonth
        strDay = Day(dtmDate)
        If Len(strDay) < 2 Then strDay = "0" & strDay
         
        strStartDate = strYear & strMonth & strDay & "000000Z"
        strEndDate = strYear & strMonth & strDay & "235959Z"
         
        strFilter = "(&(createTimeStamp>=" & strStartDate & ")(createTimeStamp<=" & strEndDate & ")(objectCategory=group))"

        Set objConnection = CreateObject("ADODB.Connection")
        objConnection.Provider = "ADsDSOObject"
        objConnection.Open "Active Directory Provider"
         
        Set objRootDSE = GetObject("LDAP://RootDSE")
        Set objRecordSet = objConnection.Execute( _
          "<LDAP://" & ROOT_OU & objRootDSE.Get("defaultNamingContext") & ">;" & _
          strFilter & ";name,createTimeStamp;subtree")
        Set objRootDSE = Nothing
         
        While Not objRecordSet.EOF
          dtmCreateTimeStamp = CDate(objRecordSet.Fields("createTimeStamp").Value)
          strMessage = objRecordSet.Fields("name") & " " & _
                objRecordSet.Fields("createTimeStamp")
         
          LogResult = LogResult & strMessage & vbcrlf 
          objRecordSet.MoveNext
        WEnd

end sub

Sub EnumPcs

LogResult = LogResult & vbNewLine & "Pcs Report:"  & vbNewLine
        dtmDate = Now
         
        strYear = Right(Year(dtmDate), 2)
        strMonth = Month(dtmDate)
        If Len(strMonth) < 2 Then strMonth = "0" & strMonth
        strDay = Day(dtmDate)
        If Len(strDay) < 2 Then strDay = "0" & strDay
         
        strStartDate = strYear & strMonth & strDay & "000000Z"
        strEndDate = strYear & strMonth & strDay & "235959Z"
         
        strFilter = "(&(createTimeStamp>=" & strStartDate & ")(createTimeStamp<=" & strEndDate & ")(objectCategory=computer))"

        Set objConnection = CreateObject("ADODB.Connection")
        objConnection.Provider = "ADsDSOObject"
        objConnection.Open "Active Directory Provider"
         
        Set objRootDSE = GetObject("LDAP://RootDSE")
        Set objRecordSet = objConnection.Execute( _
          "<LDAP://" & ROOT_OU & objRootDSE.Get("defaultNamingContext") & ">;" & _
          strFilter & ";name,createTimeStamp;subtree")
        Set objRootDSE = Nothing
         
        While Not objRecordSet.EOF
          dtmCreateTimeStamp = CDate(objRecordSet.Fields("createTimeStamp").Value)
          strMessage = objRecordSet.Fields("name") & " " & _
                objRecordSet.Fields("createTimeStamp")
         
          LogResult = LogResult & strMessage & vbcrlf 
          objRecordSet.MoveNext
        WEnd

End Sub

Open in new window

0
RimvisCommented:
But ToAddress doesn't get any value. This line is commented out, so it is not executed. Remove ' to make it work
'ToAddress = "Sharath@lth.com,Sharath@th.com"

0
bsharathAuthor Commented:
Sorry for a test i had that but i tested it without the comment
0
Sarika30Commented:
Here is the simple code to send email to multiple recipients and this is working fine.
Try this code only in a new notepad file and see if this is working for you.

Dim objEMail

Dim strEMail


   strEMail = "sarika_canon@yahoo.com;coolsarika@gmail.com"
   Set objEmail = CreateObject("CDO.Message")
   objEmail.From = "abc@xyz.com"
   objEmail.To = strEMail
   objEmail.Subject = "Welcome"
   objEmail.HTMLBody = "<b>Welcome to the Company.</b>"
       objEmail.Configuration.Fields.Item _
           ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
       objEmail.Configuration.Fields.Item _
           ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
               "smtpserverip"
        objEmail.Configuration.Fields.Item _
           ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
       objEmail.Configuration.Fields.Update
       objEmail.Send
       Wscript.echo "Mail sent to " & strEMail
0
bsharathAuthor Commented:
I have this code because i dont want to use my SMTP server details. Rather use outlook to email
0
Chris BottomleySoftware Quality Lead EngineerCommented:
SHarath

A recipient is a data structure rather than a string so try replacing:

      ' validate the recipient, just in case...
      Set myRecipient = ns.CreateRecipient(ToAddress)
      myRecipient.Resolve
      If Not myRecipient.Resolved Then
         MsgBox "unknown recipient"
      Else
         newMail.Recipients.Add(myRecipient)
         newMail.Send
      End If

with
         newMail.to = ToAddress
                        If Not newmail.Recipients.ResolveAll Then
                            For Each myRecipient In myRecipients
                                If Not myRecipient.Resolved Then
                                   MsgBox myRecipient.Name & " is unknown"
                                End If
                             Next
                        End If

Chris
0
Chris BottomleySoftware Quality Lead EngineerCommented:
SHarath

APologies ... overlooked the send if all is well.

     ' validate the recipient, just in case...
      Set myRecipient = ns.CreateRecipient(ToAddress)
      myRecipient.Resolve
      If Not myRecipient.Resolved Then
         MsgBox "unknown recipient"
      Else
         newMail.Recipients.Add(myRecipient)
         newMail.Send
      End If

with
         newMail.to = ToAddress
         If Not newmail.Recipients.ResolveAll Then
             For Each myRecipient In myRecipients
                 If Not myRecipient.Resolved Then
                     MsgBox myRecipient.Name & " is unknown"
                 End If
              Next
          Else
               newMail.Send
          End If

Chris
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sarika30Commented:
Alright. See if the following code works:

dim ToAddress,MessageSubject,MessageBody
dim ol,newMail,ns
Dim EMails(1)
EMails(0) = "Sharath@lth.com"
EMails(1) = "Sharath@lth.com"

        MessageSubject = "Test"

MessageBody = "Welcome"

        Set ol = WScript.CreateObject("Outlook.Application")
        Set ns = ol.getNamespace("MAPI")
        ns.logon "","",true,False
       
       

        ' validate the recipient, just in case...
        For Each present In EMails
            WScript.echo present
            Set myRecipient = ns.CreateRecipient(present)
            myRecipient.Resolve
            If Not myRecipient.Resolved Then
               MsgBox "unknown recipient"
            Else
                Set newMail = ol.CreateItem(0)
                newMail.Subject = MessageSubject
                newMail.Body = MessageBody
               With newMail
                    .To = present
                    .Send
                End With
                Wscript.echo "Mail Sent to " + present
            End If
        Next
Set ol = Nothing
0
bsharathAuthor Commented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.