Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
?
459 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

618 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