Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CDOSYS not sending with parameters

Status
Not open for further replies.

namida

Programmer
May 29, 2003
101
AU
Hi

I have a script (took it off from Microsoft help and did a bit of change). The original was for CDONTS and I changed it into CDOSYS.

The intention is to use it via command line CSCRIPT.

Code:
Option Explicit
On Error Resume Next

   Dim objSendMail, MyMail, oArgs, ArgNum
   Dim strTo, strFrom, strSubject, strBody

   Set oArgs = WScript.Arguments
   ArgNum = 0
 

   While ArgNum < oArgs.Count
      Select Case LCase(oArgs(ArgNum))
         Case "-to","-t":
            ArgNum = ArgNum + 1
            strTo = oArgs(ArgNum)
         Case "-from","-f":
            ArgNum = ArgNum + 1
            strFrom = oArgs(ArgNum)
         Case "-subject","-s":
            ArgNum = ArgNum + 1
            strSubject = oArgs(ArgNum)
         Case "-body","-b":
            ArgNum = ArgNum + 1
            strBody = oArgs(ArgNum)
         Case "-help","-?":
            Call DisplayUsage
         Case Else:
            Call DisplayUsage
      End Select
	WScript.Echo oArgs(ArgNum)
      ArgNum = ArgNum + 1
   Wend

   If oArgs.Count=0 Or strTo="" Or strFrom="" Or _
         strSubject="" Or strBody="" Then
      Call DisplayUsage
   Else
		Set MyMail = CreateObject("CDO.Message")
		MyMail.From = strFrom
		MyMail.To = strTo
		MyMail.Subject = strSubject
		MyMail.TextBody = strBody 
		MyMail.Send
		Set MyMail = Nothing
   End If

   ' Display the usage for this script
   Sub DisplayUsage
      WScript.Echo "Usage:"
      WScript.Echo "  sendmail -t <to address> -f <from address> -s " & _
         Chr(34) & "<subject>" & Chr(34) & " -b " & Chr(34) & _
         "<message body>" & Chr(34)
      WScript.Echo "  sendmail [-help|-?]"
      WScript.Echo ""
      WSCript.Quit
   End Sub

This code doesn't send the email for some reason. But if I replace the MyMail section into hardcoded contents it will send the email properly. This is the hardcoded version
Code:
MyMail.From = "myemail@domain.com"
MyMail.To = "myemail@domain.com"
MyMail.Subject = "sending email via CDOSYS NewMail"
MyMail.TextBody = "Sending email with CDOSYS NewMail" &_
      "objects is easy! Try it!"

The minute I change MyMail.Subject / TextBody to refer to the argument it won't send.
I echoed out each of the strSubject and strBody and it did print out the parameter properly.

Example usage : CSCRIPT sendmail.vbs -t "myemail@domain.com" -f "myemail@domain.com" -s "Subject here" -b "Some text here"

Firstly, I don't know how to print out the error, it seems Err.Number is empty.

Any insights are appreciated.



Regards,

Namida
 
if Err.Number is empty or 0 there was no error.

lose the OnError resume to allow the script to fail if there is an error and comment out .send and print the values out instead to see if there is an error in the formatting.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Hi
Yes it doesn't seem to be any different with the steps you mention. The script seemed to run perfectly fine.
The Email contents is not html or anything, they're just short strings. I even tried just sending "test" for subject and body and they were printed out fine, but not sending.

I'm thinking this might be the SMTP's side who's doing something wrong? But it runs just fine if the value was of certain texts


Regards,

Namida
 
But it runs just fine if the value was of certain texts

Then it HAS to be the variable values that are in error

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
I used your original code and your "Example usage", and it worked fine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top