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
414 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
  • 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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Make a file test.vbs using vbscript 5 62
User profile Size Report 3 63
Some AHK commands fail in Microsoft OneNote 5 28
Interview question Javascript, database 12 18
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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 …

929 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now