If you ever trying sending a with Outlook via "the back door" (e.g. by VBScript), you’ve probably found yourself facing this message :

This can be annoying if you happen to be near the computer, and could turn into a complete disaster if you’re trying to send the email from a QTP script running solo. Because of the COM interface of the outlook objects, your script will hang until this pop-up is resolved. Which could throw a night worth of testing through the window.
So, how can we work around this problem?
One solution is to install a small yet effective program called ClickYes, which, surprisingly enough, clicks yes. Most of the time this turns out to be the perfect solution : simple, effective, and doesn’t require any new code.
However, is some cases, this is not unacceptable. Maybe you’re not allowed to install any custom software, maybe there’re deployment issues, or maybe you just feel like trying the hard way for a change. Well, there is an alternative!
This solution is based on the fact that while the main VBScript in QTP is stuck, VBScript running under another proccess will continue to run uninterrupted. This means that we could run an external VBS file, just before QTP executes the command that initiates the pop-up (this usually happens upon resolving the recipients or sending the message). This VBS will loop in the background, waiting for the Outlook window to appear, send the keystrokes that resolve it, and fade the way all proper scripts do.
This actually looks something like this (remember, this code goes in an external VBS file, NOT the QTP script):
Set fso = CreateObject("WScript.Shell")
While fso.AppActivate("Microsoft Outlook") = FALSE
wscript.sleep 1000
Wend
fso.SendKeys "a", True
fso.SendKeys "y", True
wscript.sleep 7000
While fso.AppActivate ("Microsoft Outlook") = FALSE
wscript.sleep 1000
Wend
fso.SendKeys "y", True
And in the QTP script, we just put this line before executing the popup-initiation command :
SystemUtil.Run "c:\windows\system32\wscript.exe", "c:\ExternalFile.vbs"
And all will be well. By running the VBS via windows script host (wscript.exe), and not the ExecuteFile command, we ensure that the VBScript will run under a different system process, which will not hang when the security pop-up appears.
As usual, this post also was written from an answer I gave at SQAForums.

Yaron Assa




June 3rd, 2008 at 4:47 am
i think this post is very useful
June 4th, 2008 at 4:11 pm
There is one more way around. Instead of using Outlook, CDO object can be used to send a email. Which doesn’t popup any security window.
Here is the code
Const SMTPSERVER = “smtpservername”
Set Mail = CreateObject(”CDO.Message”)
Mail.From = From
Mail .To = SendTo
Mail .Subject = Subject
If UseHTML = True Then
Mail .HTMLBody = Body
Else
Mail .TextBody = Body
End If
‘ Define server / protocols
Mail.Configuration.Fields(”http://schemas.microsoft.com/cdo/configuration/smtpserver”) = SMTPSERVER
Mail.Configuration.Fields(”http://schemas.microsoft.com/cdo/configuration/sendusing”) = 2 ‘ cdoSendUsingPort
Mail.Configuration.Fields.Update
Mail.Send
June 4th, 2008 at 6:00 pm
[…] published a QTip regarding handling the Outlook Security Popup that shows whenever you try sending an E-Mail via […]
June 13th, 2008 at 2:18 pm
About 2 years ago I ran into a problem. I needed to check mail that came into a specific email box. I tried doing it via IBM lotus notes but that proved rather painful.
So, I went through Microsoft Outlook. Of course, I ran into the security problem. I’m not a big fan of Windows so that was just another reason to hate its guts. After days of banging my head against a wall, a solution presented itself that stopped me from disowning Microsoft Windows altogether!
It’s called Redemption. It’s a COM object library which once registered is accessible by any programming language. As to what it does, it circumvents windows security by duplicating the functionality of the security patch. Once you create a redemption object, you can access any properties and methods available on an original Outlook item.
For those worried that this approach might disrupt current code, no worries. You will only need one line of extra code which will basically change the way/method you declare your objects.
With this powerful COM library, you can fully automate Microsoft outlook. Tasks that can be automated include:
Email (read and send)
Contacts
Appointments
Tasks
Journal Item
Meetings
Posts
Reports
Visit the following website for further information: http://www.dimastr.com/
For those who are more interested in Microsoft Outlook automation, you will be very interested in the RDO (Redemption Data Objects) collection. Some interesting stuff!! Read the whole website for more info.
As for CDO mail?
The RDO collection looks to totally circumvent the CDO mail format which is a good thing as CDO is very limited. While I personally can use the CDO mail format, Mail administrators are particularly anal about distributing incoming and outgoing server information or assigning a mail server for my personal use (A security risk apparently). We automators are a pretty resilient bunch though. We don’t ask for favours and we work with what we have. It’s just another day at the office when some problem wants to get down right personal with you and spoil your day!
If any of the administrators are reading this, I would recommend that you devote some time to studying the Redemption library and writing some example code for all the members here. RDO version 4 is out with new interesting developments and Dimitri is always looking for ways to improve the COM library.
In the example below find some code to get email and send email. Don’t however make the mistake of inserting it in QTP and pressing F5. It’s going to pop you some nasty errors! Change the code where applicable. I’ve put in some nice comments to let you find your way…
‘=====================================================
‘close all outlook applications
systemutil.CloseProcessByName “OUTLOOK.EXE”
‘create Outlook application object
Set myOlApp = CreateObject(”Outlook.Application”)
Set myNamespace = myOlApp.GetNamespace(”MAPI”)
‘get default folder for inbox
Set myFolder =myNamespace.GetDefaultFolder(6)
‘display folder
myFolder.display
‘maximise window
myOlApp.ActiveExplorer.activate
myOlApp.ActiveExplorer.windowstate = 0
‘========================================================
‘Here you can start manipulating outlook. Check Mail or send mail
‘count number of mail items in email inbox
IntCount = myFolder.items.count
‘Time to create a redemption object which will replace our outlook object
‘Set SafeMail Redemption object
set sItem = CreateObject(”Redemption.SafeMailItem”)
For i = 1 to myFolder.items.count
‘create a object for outlook mailbox. We expecting a mail from advancedQTP to say that we have registered successfully
set oItem = myFolder.Session.GetDefaultFolder(6).Items(i) ‘get all e-mails from the Inbox, as an item collection
’synchronise inbox with redemption object
sItem.Item = oItem
‘We are checking for mail from advancedQTP to say that registration was successful. Check the sender, email address and subject to see if we have the correct email
If sItem.SenderName = “someone@advancedQTP.com” and sItem.SenderEmailAddress = “noreply@advancedqtp.com” and instr(1, sItem.body, “Thank you for registering.”) Then
‘we’ve got the correct email so lets send a return email
‘need to create a outlook mail item object
Set MailItem = myOlApp.createitem(olMailItem)
‘create a redemption object for our email that we wish to send or face the risk of the dreaded pop up!
Set SafeMailItem = CreateObject(”Redemption.SafeMailItem”)
SafeMailItem = MailItem
‘lets set some email properties
SafeMailItem.sender = “redemption@somewhere.com”
SafeMailItem.recipients = “noreply@advancedqtp.com”
SafeMailItem.subject = “Thanks for letting me register”
SafeMailItem.body = “Windows still sucks though!”
‘add an attachment for fun
SafeMailItem.attachments.add “c:\temp\windowssucks.png”
’save and send
SafeMailItem.save
SafeMailItem.send
‘quit microsoft outlook
myOlApp.quit
‘Exit for loop
Exit for
End If
Next
‘clean up
Set myOlApp = nothing
Set myNamespace = nothing
Set myFolder = nothing
set myItems = nothing
Set sItem = nothing
set oItem = nothing
set MailItem nothing
set SafeMailItem = nothing
End Function
‘====================================================
For all those who are still reading and haven’t been bored to tears… you will notice that there are some horrible problems with the piece of code I have written above.
Firstly, I didn’t log in to outlook…that’s a big problem when you have multiple email accounts in outlook.
Secondly, I didn’t do any validations of any of my parameters (for instance checking that the email address was correct before sending).
Thirdly, I didn’t put any styling in my email (Different mail clients have different mail formats. For instance, IBM notes displays different to outlook and then we have html mail clients that don’t give a crap one way or another).
The list go’s on. However, seeing that we have successfully passed the first hurdle, we can get to that other stuff later. For now, we can send and check mails even if Microsoft windows does go to extra measures to make QTP automators lives painful!
P.S., does anyone know how to do that colour coding thingy for posting code…? I looked…and looked…but inspiration to continue researching the subject was sorely lacking on my part :(. Oh well, hope you can at least follow the code I’m posting.
Cheers
J
June 13th, 2008 at 3:17 pm
I forgot to mention…
Before getting trigger happy and testing the above code, download the RDO library from the URL I specified in the precious post, register the DLL’s and then put it through its paces.
June 17th, 2008 at 9:11 am
that’s GREAT!! that’s Jason!
I have used redemption before, but only to access the Address Book, but now i can use it for send/retrieving emails, that’s awesome!