?
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
Medium Priority
?
449 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

770 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