Ask a Question

How to verify if an outlook e-mail is received

sarya
Frequent Contributor

How to verify if an outlook e-mail is received

Hi,



I am trying to write a code where I need to verify if the outlook e-mail has been received with specific body contents. I saw one script in the 'How To' section that checks if an e-mail is received but that is too complicated. Is there some easy way to put together if the e-mail with specific body contents is received in a folder ? I have this code below that checks the subject and body of the e-mail but problem is that sometimes I need to verify if an e-mail is actually received or not in the folder even when folder is empty but  the code below works when there is atleast one e-mail in the folder. So my verification is if there is a specific mail received or not even if the folder is empty .



var msOutlook = Sys.OleObject("Outlook.Application");


var mapi = msOutlook.GetNamespace("MAPI");


var inbox = mapi.Folders("Mailbox - Sumedha Arya").Folders("Inbox").Folders("Test");


for( i = 0; i <= inbox.Items.Count; i++)


{


toolEmail = inbox.Items.Item(i);


sub1 = toolEmail.Body;


sub2 = toolEmail.Subject;


Log.Message(toolEmail.Body + "And " + toolEmail.Subject);


if (sub1.indexOf(body1)!= -1 && sub1.indexOf(body2)!= -1 && sub2.indexOf(sub)!=-1)

{

Log.Message("The body and subject of the mail are verified","",pmNormal,Attr);

}

else

{

Log.Error("There is some error","",pmNormal,Attr);

}


Thanks,

Sumedha


10 REPLIES 10
YMinaev
Staff

Hi,



If your message is not received in Outlook, how are you planning to obtain it from there? If you're working with folders in Outlook, you can get messages from them, and if they're empty, you won't get any messages which is quite normal. So, it is unclear what you mean by "verify if an e-mail is actually received or not in the folder even when folder is empty" - if it is empty, this means that your message is not received.
------
Yuri
TestComplete Customer Care Engineer

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
sarya
Frequent Contributor

Hi Jared,



I am trying to automate the scenario whether I received a particular mail or not but it seems if that particular mail is not there I won't be able to automate that scenario if the mailbox is empty but what if the mailbox has some mails. I modified my script below. Where can I add that particular checkpoint when it does not find that particular message when mailbox has some items. Do I need to use if-elseif -else statements ?


function VerifyBodySubject(sub,body1,body2)

{


VerifyBodySubject(sub,body1,body2){

//'#### Set ALL INTERNAL VARIABLES



var msOutlook;

var mapi;

var inbox;

var i ;

var msOutlook = Sys.OleObject("Outlook.Application");

var mapi = msOutlook.GetNamespace("MAPI");

var inbox = mapi.Folders("Mailbox - Sumedha Arya").Folders("Inbox").Folders("Test");

Log.Message(inbox.Items.Count);

for( i = 1; i <= inbox.Items.Count; i++)

{

toolEmail = inbox.Items.Item(i);

sub1 = toolEmail.Body;

sub2 = toolEmail.Subject;

if (sub1.indexOf(body1)!= -1 && sub1.indexOf(body2)!= -1 && sub2.indexOf(sub)!=-1)

{

Log.Message(toolEmail.Body + "And " + toolEmail.Subject);

Log.Message("The body and subject of the mail are verified","",pmNormal,Attr);

toolEmail.Delete();

break;

}

else

{

continue;

}

break;

}



Thanks,

Sumedha

YMinaev
Staff

Hi,



You can check the inbox.Items.Count value before your loop. If it is zero, this means that the inbox is empty. In the if lock which will check this, you need to do what you'd do in the case if your message is not received.
------
Yuri
TestComplete Customer Care Engineer

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
sarya
Frequent Contributor

Hi Jared,



Thanks for the solution.Also I have one more query if I need to click a link in the outlook emial,how can that be automated.Is there a way we can automate clicking internal links in the outlook emails.If I just do a record,it is able to record and the script code looks like this:



Sys.Process("OUTLOOK").Window("rctrl_renwnd32", "Test - Microsoft Outlook").Window("rctrl_renwnd32").Window("rctrl_renwnd32").Window("AfxWndW").Window("AfxWndW").Window("#32770").Window("AfxWndA").Window("_WwB").Panel("Message").Click(28, 136);



Is there any way we can manipulate the script by adding our piece of code instead of using the one above?



Thanks,

Sumedha

YMinaev
Staff

Hi,



Is there any way we can manipulate the script by adding our piece of code instead of using the one above?


You can write any code you want. Just open your script in the script editor and modify it in any way you need.



As for clicking links in messages, you need to simulate user actions to do this.
------
Yuri
TestComplete Customer Care Engineer

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
sarya
Frequent Contributor

Hi Jared,



How can we stimulate user actions in outlook ? Can you give me some sample example as we need to use some control object to move in outlook.



Thanks,

Sumedha
YMinaev
Staff

Hi,



Enable MSAA or UI Automation for Outlook (see the "Using Microsoft Active Accessibility" and "Using Microsoft UI Automation Technology" help topics) - this will allow you to access more objects, properties, and methods in Outlook's process.



If this doesn't help, you'll need to use coordinate-based clicks.
------
Yuri
TestComplete Customer Care Engineer

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️

Hi

 

I am writing almost same kind of code to access outlook emails. But where I declare the outlook ole object, test complete fails as server execution failed. Its the very initial step in my script

 

I am using python

 

outlookapp = Sys.OleObject["Outlook.Application"]

 

Any help?

 

You can simply use one of the native python libraries to have a direct access to your email account (e.g. imaplib)

 

cancel
Showing results for 
Search instead for 
Did you mean: