Reading e-mail Messages - QTP

We can access e-mail messages inside a folder using the folder's Items collection as shown in the following code:

'Define the folder constant
ConstolFolderlnbox = 6
'Get the inbox folder
Set oFolder = olMAPI.GetDefaultFolder(olFolderlnbox)
'Get all the items in the folder
Set allEmails = oFolder.Items
unreadCount = 0
'Read all the unread emails
For Each email InoFolder.Items
If email.Unread = True Then
unreadCount = unreadCount + 1
End If
Next
'Display the unread email count
msgboxunreadCount

The above code displays the number of unread e-mail messages in the Inbox folder. This code loops through each message in the folder, so the code will take proportionally more time as the message count increases. But the Items collection provides a restrict method which can be used to filter emails based on various parameters, as shown in the following code:

'Get all the items in the folder
Set allEmails = oFolder.Items
'Restrict the collection to unread emails
Set unreadEmails = allEmails.Restrict!"fUnread] =" True")
'Display the count of unread emails
MsgboxunreadEmails.Count

We can use multiple restriction parameters as show in the following code:

'Restrict the collection to unread emails which have a name in the CC field
Set unreadEmails = allEmails.Restrict!" [UnreadI= True And [CC] O "")
'Restrict the collection to unread emails which have a specific subject
Set unreadEmails =allEmails.Restrict!" [UnreadI = True And [Subject! = "Your Order Confirmation'")
'Restrict the collection to unread emails from a specific sender
Set unreadEmails = allEmails.Restrict!"[UnreadI = True AndIFronsl="TarunLalwani"")
'Restrict the collection to unread emails NOT from a specific sender
Set unreadEmails = allEmails.Restrict!"[UnreadI = True And NOT [From!='Tarun Lalwani")

The Restrict method supports OR, AND, NOT operators. But there is no LIKE operator which means we cannot filter based on regular expression syntax. In such cases the filter should be set to limit the emails collection to a minimum and then loop through the selected messages to get just those we are looking for. The Unread flag is commonly used to limit the collection and in case the email is not required again it can then be marked as Read using the following code:

'Restrict the collection to unread emails NOT from 'TarunLalwani'
Set unreadEmails = allEmails.Restrict!"[Unread] = True And NOT [Rronls'TarunLalwani"")
For Each email InunreadEmails
email.UnRead = False
Next

Accessing e-mail Message Properties

Once we have an e-mail message we can access its various properties. Some of these properties are shown in the Table :

Accessing e-mail Message Properties

Downloading attachments

An e-mail message's attachments can be downloaded by accessing its Attachments collection, using the following code:

'Get the email object from a folder
Set email = oFolder.items(1)
'Download all the attachments to "C:Test" folder
For Each attachment Inemail.Attachments
attachment.SaveAsFileTtYTestN" &attachment.FileName
Next

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

QTP Topics