Sending email with vbscript thru Exchange Server

I have a remote desktop that I process files on.  I'm trying to automate.  I have an account on the company exchange server no different then anyone elses.  Is it possible to send myself and email using vbscript through my exchange server account.

There isn't outlook or anything on the machine so I don't do email on that machine now - if that matters

thanks
keschusterAsked:
Who is Participating?
 
Robberbaron (robr)Connect With a Mentor Commented:
I have reformated the code I origianlly posted to be a vbs file.
You can call the vbs from a bat file like cscript sendmail.vbs xx yy zz qq

you would need to check on how to get command line parameters xx yy zz in the vbs file. I have it somewhere but cant recall off the top of my head.

test message works on my box here. Well sort of at least.... The desktop virus scanner pops up notifying it is scanning a message. But server IMF filter drops message as being junk, which it is !


see seach MS site for CDO & CDO.SYS
... for use of CDO.SYS which is a little newer.
http://forums.whirlpool.net.au/forum-replies-archive.cfm/495986.html

ASP code is very similar to VBS.

 
dim msgTo, msgFrom, sMailServerName, msgTxt, msgAttPath
 
	'set data for msg
	msgTo = "keschuster@test1.com "  'test@qwerty.com"
	msgFrom = "robberbaron@qwerty.com"  '"mail@zxcvbn.com.au"
	msgSubject = "Test message of VBScript"
	msgTxt = "test #1" & vbcrlf & "cdo send"
	msgAttPath = ""
	SendMail msgFrom, msgTo, msgSubject, msgTxt, msgAttPath
	
	wscript.quit
 
'-------------------------------------------------------
Sub SendMail ( sFrom, sTo, sSubject, sBody, sAttachPath)
	
	'allocate variables
	Dim cdoSendUsingPort, cdoAnonymous,cdoDSNSuccessFailOrDelay
	dim objMsg, objConf
	dim tcSubject, lcAttName 
	
	'set constants as VBS doesnt get from Object
	cdoSendUsingPort = 2
	cdoAnonymous = 0
	cdoDSNSuccessFailOrDelay=14
	
	'set server data
	sMailServerName = "192.168.1.1"     'exch.qwertyx.com.
	
 
	
	
	Set objConf = CreateObject("CDO.Configuration")
			
		With objConf.fields
		  .item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
		  .item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = sMailServerName
		  .item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoAnonymous 'cdoBasic
		  '.item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "your-username"
		  'item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "your-password"
		  .Update
		End With
		
		Set objMsg = CreateObject("CDO.Message")
		objMsg.Configuration = objConf
	 
	 
		With objMsg
	 
		  
		  .to = sTo
		  .From = sFrom
		  .Subject =  sSubject & " " 
		  .TextBody = sBody
		   'use .HTMLBody to send HTML email.
	  
		  if sAttachPath <> "" then
			   .AddAttachment sAttachPath
		  end if
		  
		  .fields("urn:schemas:mailheader:disposition-notification-to") = sFrom
		  .fields("urn:schemas:mailheader:return-receipt-to") = sFrom
	 
		  .DSNOptions = cdoDSNSuccessFailOrDelay
		  .fields.Update
		  .Send
		End With
	 
		set objConf = Nothing
		Set objMsg = Nothing
END SUB

Open in new window

0
 
kieran_bCommented:
Are you sending to yourself INSIDE the network?  Or to ask another way, is the exchange server you are wanting to send through responsible for the address you are sending to?

If so, then you won't need to authenticate, it will be as simple as pointing any VBScript to the Exchange server and using it like a normal SMTP server

I am no scripter, but the first link I found on doing this showed good results;

http://forums.devx.com/showthread.php?threadid=21954

As you don't have Outlook, skip suggestions 2 and 3, but 1 and 4 are good options.

Kieran
0
 
Robberbaron (robr)Commented:
CDO works for me generally.  used from Excel.


    Set objConf = CreateObject("CDO.Configuration")
        
    With objConf.fields
      .item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
      .item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = sMailServerName
      .item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoAnonymous 'cdoBasic
      '.item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "your-username"
      'item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "your-password"
      .Update
    End With
    
    Set objMsg = CreateObject("CDO.Message")
    objMsg.Configuration = objConf
 
 
    With objMsg
 
      
      '.to = "test@qwerty.com"
      .From = "mail@zxcvbn.com.au"
      .Subject = Space(2) & Trim(tcSubject)
      .TextBody = "what"
       'use .HTMLBody to send HTML email.
  
           .AddAttachment lcAttName
      
      .fields("urn:schemas:mailheader:disposition-notification-to") = "mail@zxcvbn.com.au"
      .fields("urn:schemas:mailheader:return-receipt-to") = "mail@zxcvbn.com.au"
 
      .DSNOptions = cdoDSNSuccessFailOrDelay
      .fields.Update
      .Send
    End With
 
    Set objMsg = Nothing
    NewMail = True

Open in new window

0
Easily manage email signatures in Office 365

Managing email signatures in Office 365 can be a challenging task if you don't have the right tool. CodeTwo Email Signatures for Office 365 will help you implement a unified email signature look, no matter what email client is used by users. Test it for free!

 
keschusterAuthor Commented:
I'm running from vbs script on my desktop within the network.

Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Example CDO Message"
objMessage.From = "myemail@email.com"
objMessage.To = "myemail@email.com"
objMessage.TextBody = "This is some sample message text."
objMessage.Send

generates this error
The "SendUsing" configuration value is invalid"
Code: 80040220
Source: CDO.Message.1

Any ideas?
0
 
WylkellCommented:
You need to specify the mail server you want to send the message via:

objMessage.SMTPServer = "myserver.mydomain.com"


the string can be a shortname, a FQDN, or an ip address, as long as it's routeable.
0
 
keschusterAuthor Commented:
Set objMessage = CreateObject("CDO.Message")
objMessage.SMTPServer  = "server.COM"
objMessage.Subject = "Example CDO Message"
objMessage.From = "email@email.com"
objMessage.To = "email@email.com"
objMessage.TextBody = "This is some sample message text."
objMessage.Send


Now I get error - Object doesn't support this property or method:'objMessage.smtpserver'
0
 
WylkellCommented:
oops, my bad.  .SMTPServer is a property of the CDO configuration, not the message.  you'd need to specify that first.  I'd write the code for you but robberbaron already has.  Look at lines 1-13 on the script he posted.  Those should work for you (modified for your needs of course)
0
 
keschusterAuthor Commented:
I found solution using activex
google Chilkat
0
 
Robberbaron (robr)Commented:
AspEmail does the same sort of thing. It has a freeware component. chilkat is not.

CDO is lower level than these and is MS download for use with Exchange.
0
 
keschusterAuthor Commented:
"CDO is lower level than these and is MS download for use with Exchange."

for use with exchange?  Were do I get more information on this?  I can use this to send email via BAT file for VBS?
0
 
keschusterAuthor Commented:
I get an error message on line 65
"The Server rejected one or more recipient addresses.  the server reponse was:550 5.7.1. Unable to relay for Keithschsuter@gmail.com

I'm running this on the company network and pointing to the exchange server.
Any ideas?
0
 
keschusterAuthor Commented:
I changed authentication to = 2 and it WORKED!!!
Thanks
0
All Courses

From novice to tech pro — start learning today.