Friday, August 21, 2009

QTP Script to send email from Microsoft Outlook

The below script opens Outlook, sends an email message and closes Outlook. (I have used Outlook 2007). Write the below code in a new test in QTP and run it. Do not forget to write your email address below.

'using the CreateObject method to create an Outlook Application object.
Set olApp = CreateObject("Outlook.Application")
'GetNameSpace - Returns a NameSpace object of the specified type. It exists because outlook was designed to be usable with different kinds of data, each of which would be identified by its own namespace. So far, only one kind of data is supported in Outlook, MAPI data (Messaging Applicaiton Programming Interface).
Set olns = olApp.GetNameSpace("MAPI")
'logging into outlook application and displaying it
olns.Logon
Set objFolder = olns.GetDefaultFolder(6)
objFolder.Display
'below we are creating a new email message, assigning a recipient, subject etc. and sending it.
Set msg = olApp.CreateItem(olMailItem)
Set Recpt = msg.Recipients.Add("sachin@gmail.com")
msg.Subject = "Test Subject"
msg.body = "Test Body"
msg.send
'waiting for some time till the Outlook sends the message before closing it. Needed if network is slow.
Wait(30)
olApp.quit


If during running of the script you receive a message box like this



Then Go to Help (menu) -> Privacy Options

Go to Programmatic Access...

Select "Never warn me about suspicious activity" radio button.

Also See:

QTP & Microsoft Outlook Object Model
QTP Script to get inbox folder name in messagebox
QTP Script to count emails in any Outlook folder
QTP Script to enumerate all folders in Outlook
QTP Script to send an email with attachment from Outlook
QTP Script to read an email from Outlook
QTP Script to download an email attachment from Outlook