vbscript to send smtp mail

gateguard
gateguard used Ask the Experts™
on
How do I script the following commands, instead of having to type them one-by-one in command line?

If I type these commands in command line, the mail is sent:

telnet mail.test.com 25
EHLO test.com
MAIL FROM:Admin@test.com
RCPT TO: User@test.Com
DATA
Subject: test message
text
text

.
QUIT


(mail.test.com is a mail server on my LAN)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You can use an alternative way, use the code like this one:
http://www.freevbcode.com/ShowCode.Asp?ID=109

Author

Commented:
I get this error:

C:\sendmail.vbs(4, 1) Microsoft VBScript runtime error: Class not defined: 'vbSendMail'
Most Valuable Expert 2012
Top Expert 2014
Commented:
You cannot script Telnet directly, but you could try Blat.exe from http://www.blat.net/
and then run a vbs with something like:
'==================================
      strMessage = "Body text"
      strSubject = 'Subject line"'
      strSender = "somebody@@mysite.local"
      strRecipients = "somebody1@@mysite.local, somebody2@@mysite.local"      
      strExchangeServer = "Your_server_NETBIOS_NAME or IP"
      Set objShell = CreateObject("Wscript.Shell")
      objShell "%comspec% /c Blat " & strMessage & " -f " & strSender & " -t " & strRecipients & " -server " & strExchangeServer & " -s " & strSubject & " -q", 1, True
'=========================

But I haven't tested this, so let me know how you go....

Regards,

Rob.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

>> I get this error:
>>C:\sendmail.vbs(4, 1) Microsoft VBScript runtime error: Class not defined: 'vbSendMail'

Maybe you have not registered the vbsendmail.dll, try registering that dll file,
run this from the command:

regsvr32 vbsendmail.dll
Most Valuable Expert 2012
Top Expert 2014

Commented:
Mortaza, the way that the sendmail code is written in the link you provided, it looks like strict VB6, and would not be compatible with VBScript because of the type declarations.  Gateguard, if you have VB6, run this code in that.

Otherwise, my vbs script should run (as long as you have blat.exe) on Windows 2000 or later.

Regards,

Rob.
>>Mortaza, the way that the sendmail code is written in the link you provided, it looks like strict VB6, and >>would not be compatible with VBScript because of the type declarations.

Yes, that code is strict VB6

I thought that could help...
Commented:
Try this code.  It's not telnet, it uses the outlook application installed on your system.

Dim ToAddress
Dim MessageSubject
Dim MessageBody
Dim MessageAttachment

Dim ol, ns, newMail

ToAddress = "John.Smith@place.com"   ' change this...
MessageSubject = "My Subject"
MessageBody = "DATA"

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

 

Author

Commented:
I went with the Outlook solution since I do have Outlook installed on all the machines I'm running this script on.  But thanks for the other ideas also.  I might use them in other cases.

Author

Commented:
Rob,  I am going to test blat and I will let you know.  I'll put a comment in here.
If you want to use VBScript here is a script that works using CDO:


Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory.
Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network).

Const cdoAnonymous = 0 'Do not authenticate
Const cdoBasic = 1 'basic (clear-text) authentication
Const cdoNTLM = 2 'NTLM

Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Test Message"
objMessage.Sender = "user@domain.com"
objMessage.From = "user@domain.com"
objMessage.To = "recipient@domain.com"
objMessage.TextBody = "Test"

'==This section provides the configuration information for the remote SMTP server.

objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.domain.com"

'Type of authentication, NONE, Basic (Base64 encoded), NTLM
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoAnonymous

'Your UserID on the SMTP server
'objMessage.Configuration.Fields.Item _
'("http://schemas.microsoft.com/cdo/configuration/sendusername") = "user@domain.com"

'Your password on the SMTP server
'objMessage.Configuration.Fields.Item _
'("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"

'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

'Use SSL for the connection (False or True)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False

'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60

objMessage.Configuration.Fields.Update
objMessage.Send

If Err.number <> 0 then
      response.write Err.number & " " & Err.Description & "<BR>"
End If

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial