QTP v10 Learn Online - On Sale Today

22
Jan 10

QTP: Send Outlook Email

Category: QuickTest Pro, Sample Code |

QuickTest Pro sample code – sending an email using Outlook

This QuickTest Pro code sample shows how you can create a subroutine that will send an email using Microsoft Outlook.  The subroutine is defined and can either be placed at the top of your script (must be executed first before it is called in order for the script to know about the subroutine) or it can be placed in a Function Library and associated with the test script. 

The reason to use a subroutine is to isolate the process code that creates and sends the Outlook email to a common subroutine in order to take advantage of reuse.  This way every time a test script needs to send an email it just needs to have the bottom three lines of code shown below in the code image (lines 35 – 37).  It utilizes the “CreateObject” to create an instance of the Outlook application in order to create the actual email.  The data used in the email is passed into the subroutine using an array and then each element is referenced within the subroutine code.

Outlook email code

Here is the result of sending the email, the email was received and it looks like this:

Outlook sample email

This is kind of a neat feature that you may find a need for.

In addition to the images above, here is a snippet of the code for easier readability and the ability to copy the code for your own use:



Sub CreateEmail (emailInfo)
Dim objOutl, objMailMsg, objNameSpace, intRecipientCounter, strEmailAddressConst olMailItem = 0      ’ Constants for new items
Const olAppointmentItem = 1
Const olContactItem = 2
Const olTaskItem = 3
Const olJournalItem = 4
Const olNoteItem = 5
Const olPostItem = 6
intRecipientCounter=1      ’Start the email address counter at 1Set objOutl=CreateObject (“Outlook.Application”)   ’Create an Outlook object
Set objMailMsg=objOutl.CreateItem(olMailItem)      ’Create a new Outlook mail object
objMailMsg.Recipients.Add(emailInfo(0))         ’Add the email address to the recipient list of the message
objMailMsg.Subject=emailInfo(1)            ’Set the subject of the message
objMailMsg.Body=emailInfo(2)            ’Set the body of the mail message
objMailMsg.Send               ’Send the mail message
‘Free up the memory
Set objOutl=Nothing
Set objMailMsg=Nothing
End Sub‘Script code to actually call the subroutine with data to be used in the email that will be sent
Dim emailInfo
emailInfo = Array(“test@hotmail.com”,”Subject: QTP Test email”,”Desc: This is a test email sent by QTP”)
CreateEmail (emailInfo)

Good Luck.


by Shawn | About the author: Shawn LoPorto brings 23 years of diverse technology and business experience with strong technical skills, leadership, problem-solving, planning, team development and project management skills. Shawn is a Vice President of QA & CM. See the Profile page on this website for more information.

Related Posts


Comments


Posted on Friday, January 22nd, 2010 at 11:32 am and is filed under QuickTest Pro, Sample Code. You can follow any responses to this entry through the RSS 2.0 feed. Responses are currently closed, but you can trackback from your own site.
3 Comments so far

  1. 1 Bharath on April 9, 2010
  2. 2 Los Gatos Dentist on May 27, 2010

    It was an absolute pleasure to read through your piece with all the interesting topics you write about. My imagination went wild with all the detailed words you used. Please carry on to grace us with your work as it is really enjoyed.

  3. 3 GuisipRaxpoox on June 7, 2010

    That was a really interesting post, I enjoyed reading it. You are dead right!

Name (required)

Email (required)

Website

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Share your wisdom