Monday, June 04, 2012

How to send a email using VB Script.

Some times requirements occurs to send email from Windows environment. it may be due to server monitoring or application monitoring purpose or may be some other need. Lot of third-party applications in the internet and you can try those your own. But most light weight and slandered way to send email from windows environment is the VB script. Using VB Script is very easy and simple. following is the way to send a email using VB Script. this can be help to your script development and system monitoring purpose.


Dim objMail

Set objMail = CreateObject("CDO.Message")

'sender email address
objMail.From = "Sender <sender@abcmail.com>"

'Receiver email address
objMail.To = "Receiver <receiver@abcmail.com>"

'Email subject
objMail.Subject = "This is the testing email subject"

'Email Body
objMail.Textbody = "This is the my first email send by vb script"

'If you need to attache a file,can do in this way.
objMail.AddAttachment "C:\myphoto.jpg"

'Configure SMTP server
objMail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

'Here you should configure name or IP of the SMTP server
objMail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
    "192.168.1.23"

' SMTP port need to configure here (Default 25)
objMail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

'update the field in message object 
objMail.Configuration.Fields.Update

'sending email
objMail.Send

'clear mail object
Set objMail=Nothing

'exit the script
Wscript.Quit