Wednesday 15 January 2014

Automating outlook for accessing mails and mail attachments using TestComplete

Hi,


Here I will be discussing on handling/automating Microsoft outlook using TestComplete:

Though there are many documents that details on handling outlook mail items using TestComplete. I couldn't find references on handling attachments that come with the mail.
Here is the code snippet for the same:
   
function isMessageReceived2010(accountName, senderEMail, eMailSubject)

{

var OutlookApplication =

 

Sys.OleObject("Outlook.Application");  

var NamespaceMAPI = OutlookApplication.GetNamespace("MAPI");

var abc=NamespaceMAPI.Accounts.Item(1);

// Check whether the specified account exists:

if (NamespaceMAPI.Accounts.Item(accountName) != null)

{

NamespaceMAPI.SendAndReceive(false);

var targetfolder="c:/";

// Get the "Inbox" folder 

var inbox = NamespaceMAPI.Folders(accountName).Folders("Inbox");

//get all mail items and store it in a variable

var items = inbox.Items; 

aqFile.WriteToTextFile("c:/sample/abc.txt",items.Count,aqFile.ctUTF8);

 //traversing through the mail items

for (var i = items.Count; i >= 1; i--)

{

//get the mail subject

aqString.Trim(items.Item(i).Subject,aqString.stAll);

//you can also get any other details on mail, as required 

aqString.Trim(items.Item(i).SenderEmailAddress,aqString.stAll);

 

 

if (items.Item(i).Subject == eMailSubject)

{

//get the number of attachments that come with the expected mail

aqFile.WriteToTextFile("c:/sample/abc.txt",items.Item(i).Attachments.Count,aqFile.ctUTF8);

 

var myItem=items.Item(i);

if(myItem.Attachments.Count>0)


aqFile.WriteToTextFile("c:/sample/abc.txt","do i have attachments,my count is "+ myItem.Attachments.Count,aqFile.ctUTF8);

//get the attachment name and print it 

attachmentName=myItem.Attachments.Item(1);

aqFile.WriteToTextFile("c:/sample/abc.txt","my attachment name" + attachmentName,aqFile.ctUTF8);

//save your attachment in the preferred path and preferred format

attachment.SaveAsFile("c:/sample/as.pdf");

/* Now, in case if the attachment has to be compared with an other item, use TestComplete checkpoint concept, by storing the base lined document in stores compare it with the attachment that is downloaded from mail – u r done! */

Files.PDS_IAL_SGIO_pdf.Check("C:\\sample\\as.pdf"); 

return true;

}

}

}

return false;

} else

{

OutlookApplication.Quit();

return false;

}

 
Hope this snippet helps :-)