Solved

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

Posted on 2010-08-16
14
443 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:bsharath
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
  • +1
14 Comments
 
LVL 19

Expert Comment

by:Rimvis
ID: 33451791
Just a guess:a)try  to remove space between "com" and ";"b)use "," instead of ";"
0
 
LVL 11

Author Comment

by:bsharath
ID: 33451810
Did not work
0
 
LVL 4

Expert Comment

by:Sarika30
ID: 33451843
Try this:
Recipient = "you@yours.com;me@mine.com"
0
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
LVL 11

Author Comment

by:bsharath
ID: 33451882
Still no. It does not work
0
 
LVL 4

Expert Comment

by:Sarika30
ID: 33451906
Can you please post the full code you are using for sending mail?
0
 
LVL 11

Author Comment

by:bsharath
ID: 33451927
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
 
LVL 19

Expert Comment

by:Rimvis
ID: 33451947
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
 
LVL 11

Author Comment

by:bsharath
ID: 33451952
Sorry for a test i had that but i tested it without the comment
0
 
LVL 4

Expert Comment

by:Sarika30
ID: 33451964
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
 
LVL 11

Author Comment

by:bsharath
ID: 33451971
I have this code because i dont want to use my SMTP server details. Rather use outlook to email
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 33451992
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
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 total points
ID: 33452003
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
 
LVL 4

Expert Comment

by:Sarika30
ID: 33452074
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
 
LVL 11

Author Comment

by:bsharath
ID: 33452091
0

Featured Post

Containers and Docker for Everyone

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

688 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