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, an email 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, 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 my free VBScript tutorials. Thank you.

11 comments:

  1. thanks sir. It is very helpful to me.

    ReplyDelete
  2. its simple and nice script...couls you please share some case in which we can search some data in the send mail

    ReplyDelete
  3. how to select a file for attachment using regular expression, because in my case multiple files with same prefix exists in that folder with different date and time stamp?

    ReplyDelete
    Replies
    1. An alternate way to attach multiple files is to repeat the similar command within a For Next loop. Refer https://inderpsingh.blogspot.com/2020/03/VBScriptTutorial5.html

      Delete
  4. Thanks for the prompt reply.
    Actually I want to send only the latest generated file, as an attachment from a folder which contains multiple files with same prefix of 10 letters and from 11th letter there will be todays date in the file name before the file extension.
    Thanks

    ReplyDelete
    Replies
    1. I tested the following VBScript code. It gave me the name of the latest file in my folder (given on line 4 below).
      Dim objFSO, objFile
      Set objFSO = CreateObject("Scripting.FileSystemObject")
      Set latestFile = Nothing
      For Each objFile in objFSO.GetFolder("E:\Training\VBScript").Files
      If latestFile is Nothing Then
      Set latestFile = objFile
      ElseIf objFile.DateCreated > latestFile.DateCreated Then
      Set latestFile = objFile
      End If
      Next
      MsgBox latestFile.Name

      Delete
    2. The comment above removed the indentations in the For and If statements. You should reinstate them.

      Delete
  5. Thank you so much for your efforts. It solved my problem.

    ReplyDelete
    Replies
    1. If you need any technical support, my contact details are on my YouTube channel at the link, https://www.youtube.com/user/SoftwareTestingSpace/about

      Delete
  6. Excellent !!! It works very well. Thanks.

    ReplyDelete

Note: Only a member of this blog may post a comment.