automationScript

Automatic email sender Outlook automation using VBScript

Everyday we use outlook to send so many mails. One of the most fantastic ways to automate sending a mail is through VBScript. We can use this for so many use cases for example:

  1. To send log files to predefined email addresses (or stakeholders) after a test execution.
  2. To trigger a test execution based on certain predefined subject line.
  3. Share failed screenshots to developers or BA as a routine.
  4. Send a request at a predefined time everyday with the help of task scheduler.

You get the idea right!

Here it is, an example to write your own custom automatic email sender outlook automation function to send a mail from outlook through VBScript.

Code Explanation

In this code snippet, we use inbuilt object to interact with the outlook application through VBScript.

Set ol=CreateObject("Outlook.Application")

You can pass the following as String arguments.

  • To Address
  • Email Subject
  • Body text
  • Attachment

In the code above, you might have noticed the following line

Set Mail=ol.CreateItem(0)

This is the line where we create an object of Mail type to work on emails. Apart from sending emails, outlook provides so many functionalities, like creating a distribution list, creating appointment, meeting etc.

As we can send a mail in outlook using VBScript, we can also perform other tasks. You need to create an object of some other type to work on these things.

Please check the below table to understand what ItemType can be used for which purpose.

For example: If you want to work with Contacts, You need to create an object like below.

Set oContact=ol.CreateItem(2)

List of other Item types in Outlook

ItemType ValueItemType NameDescription
0olMailItemA MailItem object.
1olAppointmentItemAn AppointmentItem object.
2olContactItemA ContactItem object.
3olTaskItemA TaskItem object.
4olJournalItemA JournalItem object.
5olNoteItemA NoteItem object.
6olPostItemA PostItem object.
7olDistributionListItemA DistListItem object.
Subscribe for updates