Everyday we use outlook to send so many mails. This can be automated by outlook automation using VBScript. 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:
- To send log files to predefined email addresses (or stakeholders) after a test execution.
- To trigger a test execution based on certain predefined subject line.
- Share failed screenshots to developers or BA as a routine.
- 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)
ItemType Value | ItemType Name | Description |
---|---|---|
1 | olMailItem | A MailItem object. |
2 | olAppointmentItem | An AppointmentItem object. |
3 | olContactItem | A ContactItem object. |
4 | olTaskItem | A TaskItem object. |
5 | olJournalItem | A JournalItem object. |
6 | olNoteItem | A NoteItem object. |
7 | olPostItem | A PostItem object. |
8 | olDistributionListItem | A DistListItem object. |
List of other Item types in Outlook