Saturday, April 20, 2013

How to send an email using VBScript?


In the last article, we have learnt that VBScript is useful to write small automation utilities like writing commands in cmd.exe, launching an executable, killing all instances of an executable etc. But do you remember a forwarded email that contained an attachment file and sender asked you to save that file with .vbs extension and then to double click that file to see some magic? If you do perform those steps, a program automatically executes, open a new email message, write recipient email address in "To" field, write some silly text in body and then sends that mail using your Outlook. By the time you understand something and react, mail shoots to recipient. Actually that attachment could be a VBScript. We will learn how such a script works.
If you want to learn VBScript from basics to some advanced topics, you are welcome to use my free VBScript tutorial for beginners.

First we have to create an object of Outlook.Application class (This only works if you have Outlook configured with an email address in your machine) and then create an object of mail item. The code snippet follows. Note that all the source code is in italics font.

   Set objOutlook = CreateObject("Outlook.Application")
   Set objMail = objOutlook.CreateItem(0)

To see the email message, use .Display method, otherwise processing will be happen in background.
   objMail.Display   'To display message

Now fill Recipient email id, Copy email id, Subject and Body, using following commands:
   objMail.to = "abc@example.com"
   objMail.cc = "pqr@example.com"
   objMail.Subject = "Mail Subject"
   objMail.Body = "This is Email Body"

To add attachments, one can use following command (Repeat similar command to add more attachments):
   objMail.Attachments.Add("C:\Attachment\abc.jpg")

Now email is ready, now use .Send method to send your message to recipient:
   objMail.Send

Now open notepad and integrate all these to see how it works. Below is the complete script to send email message (I intentionally commented the objMail.Send line below, so you are not surprised. If you want to actually send the mail, just uncomment objMail.Send line):

   Set objOutlook = CreateObject("Outlook.Application")
   Set objMail = objOutlook.CreateItem(0)
   objMail.Display   'To display message
   objMail.To = "recipient@example.com"
   objMail.cc = "copyrecipient@example.com"
   objMail.Subject = "Test Mail Subject"
   objMail.Body = "This is Email Body"
   'objMail.Attachments.Add("C:\Attachment\abc.jpg")   'Make sure attachment exists at given path. Then uncomment this line.
   'objMail.Send   'I intentionally commented this line
   objOutlook.Quit
   Set objMail = Nothing
   Set objOutlook = Nothing
Save notepad file as .vbs extension. Double click .vbs file to execute VBScript.

To send a mail to multiple recipients, use following commands instead of objMail.To line:
   objMail.Recipients.Add("recipient1@example.com")
   objMail.Recipients.Add("recipient2@example.com")
   objMail.Recipients.Add("recipient3@example.com")

Below is the complete script is to send mail to multiple recipients (I intentionally commented the objMail.Send line below, so you are not surprised. If you want to actually send the email, just uncomment objMail.Send line). Write below code in a notepad file, save it as .vbs extension and then double click to execute script.

   Set objOutlook = CreateObject("Outlook.Application")
   Set objMail = objOutlook.CreateItem(0)
   objMail.Display   'To display message
   objMail.Recipients.Add ("recipient1@example.com")
   objMail.Recipients.Add ("recipient2@example.com")
   objMail.Recipients.Add ("recipient3@example.com")
   objMail.Subject = "Mail Subject"
   objMail.Body = "This is Email Body"
   'objMail.Attachments.Add("C:\Attachment\abc.jpg")   'Make sure attachment exists at given path. Then uncomment this line.
   'objMail.Send   'I intentionally commented this line
   objOutlook.Quit
   Set objMail = Nothing
   Set objOutlook = Nothing

Please provide your feedback. Thanks!

Note: This article has been contributed by Vinod Joshi, who is a test automation professional. Vinod is a respected test automation designer. He is very active in the software testing space community. Additionally, Vinod is the moderator of the Software Testing Space group in LinkedIn.

Want to learn VBScript in detail? Use our free VBScript tutorials.