elmangaso has asked for the wisdom of the Perl Monks concerning the following question:

Over the Christmas break, I decided to tackle a small project that may have been a little beyond my perl skills. I wanted a win32 perl process to monitor incoming Outlook email and notify another perl process on a different network machine(linux). With plenty of FAQ searches, googling, etc. I managed to get 90% of what I needed...now I seek the wisdom of the perlmonks. Read on for some background on why I'm trying to do this and a description of the problem...

...many moons ago, my work mail was sent exclusively through a unix based mail server. I worked on a linux machine and used pine, exmh, then netscape mail for awhile. Slowly though, my workgroup and the rest of the company moved to Exchange servers/Outlook mail for all mail/meeting stuff. For awhile I forwarded everything to the unix mail, but eventually I got more and more excel/word/ppt/ attachments , having to log into a windows terminal server session to invoke outlook periodically to check my exchange mail. Now I keep a wts session with outlook running(using rdesktop) up in one of many virtual desktops (fvwm2) So...

My problem though was that when working in one of the other virtual dekstops, I wasn't notified of new outlook mails/reminders. I wanted a little xbiff like widget that notified me of new mail with perhaps even a subject preview. Miricle of Miricles, I noticed that they recently installed activestate perl on the wts machines. So I decided to try and use a win32/ole interface to notify me of incoming mail or reminders and a seperate process would launch a simple tcp server that would handle 2 clients. The win32 process(client1) detects an outlook event, notifies the tcp server, then the server notifies a remote perl/tk app(client2). My problem is that when I attempt to retrieve the mail sender or address property I get an Outlook security prompt:
my $namespace = $outlook->GetNamespace("MAPI"); my $folder = $namespace->GetDefaultFolder(olFolderInbox); my $mailitems = $folder->Items; my $firstmail = $mailitems->GetFirst; my $subject = $firstmail->Subject; my $sendername = $firstmail->SenderName; #Outlook security prompt
I found a description of possible workarounds at this site:
http://www.outlookcode.com/d/sec.htm

-but they all involve either installing a 3rd party app (I'm not allowed to touch the windows registry on these wts machines) or they are beyond my meager skills. Does anyone know a way around this? I can probably live without knowing the sender name on a message pop up, but I figured if anyone knew how do do this in perl, it would be someone reading perlmonks...and tips would be much appreciated.

I'll post the code of the win32 perl script (active state 5.6.1) that monitors outlook and launches a tcp server:
#!/usr/bin/perl use strict; use Win32::Process; use Tk; use Win32::OLE qw(EVENTS); use Win32::OLE::Const 'Microsoft Outlook'; use IO::Socket; use subs qw(dump_calendar Event GetMessage ErrorReport exit_app); require Tk::ROText; my $homelog = "H:/outlook_calendar.dump"; my $ProcessObj; Win32::Process::Create($ProcessObj, 'C:\Perl\bin\perl.exe', 'perl H:\b +in\receiver.pl', 0, DETACHED_PROCESS , ".")|| die ErrorReport(); ######client coonection to above server process my $socket = IO::Socket::INET->new( PeerAddr => 'localhost', PeerPort => '8008', Proto => 'tcp', ); defined $socket or die "ERROR: Can't connect to port 8008\n"; #Get the active outlook process my $outlook = Win32::OLE->GetActiveObject('Outlook.Application') or di +e "Error! $!\n"; #Event handler for newmails/reminders/etc Win32::OLE->WithEvents($outlook,\&Event,"ApplicationEvents_10"); ######################################### #Create a basic window with a text widget my $mw = new MainWindow; my $mw_text = $mw->Scrolled(qw/ROText -scrollbars ose/)->pack; my $mw_entry = $mw->Entry()->pack(qw/-fill x -pady 5/); $mw->bind('<Any-Enter>' => sub { $mw_entry->Tk::focus }); #$mw_entry->bind('<Return>' => [\&broadcast, $socket]); $mw_entry->bind('<Return>' => sub { my $text = $mw_entry->get; $mw_entry->delete(qw/0 end/); print $socket "$text\n"; }); $mw->protocol('WM_DELETE_WINDOW', \&exit_app); MainLoop; ########################################### #Win32::OLE->MessageLoop(); #Don't need it? already in tk mainloop sub broadcast { my ($ent, $sock) = @_; my $text = $ent->get; $ent->delete(qw/0 end/); print $socket $text, "\n"; } sub ErrorReport{ print Win32::FormatMessage( Win32::GetLastError() ); } sub dump_calendar { my $namespace = $outlook->GetNamespace("MAPI"); my $folder = $namespace->GetDefaultFolder(olFolderCalendar); my $items = $folder->Items; #Format a date string to use for a Start time filter### my ($dsec,$dmin,$dhour,$dmday,$dmon,$dyear,$dwday,$dyday,$disdst) = +localtime(time); my $fixed_year = $dyear - 100; my $fixed_month = $dmon + 1; my $mon = sprintf("%02d", $fixed_month); my $day = sprintf("%02d", $dmday); my $year = sprintf("%02d", $fixed_year); print "DATE: $mon : $day : $year\n "; my $date_string = "$mon/$day/$year"; ####################################################### my $strToday = "[Start] >= '" .$date_string ." 12:00 am' and [Start] + <= '". $date_string . " 11:59 pm'"; $items->Sort("[Start]"); $items->{IncludeRecurrences} = 1; my $appts = $items->Restrict("$strToday"); $appts->Sort("[Start]"); open (HL, ">$homelog") or die "Couldn't open $homelog: $!\n"; my $current_item = $appts->GetFirst; while ($current_item){ #print "Subject = " . $current_item->{Subject} . "\n"; print HL "Subject = " . $current_item->{Subject} . "\n"; print HL "Start = " . $current_item->{Start}->Date . " " . $curre +nt_item->{Start}->Time ."\n"; print HL "Location = " . $current_item->{Location} . "\n"; $current_item = $appts->GetNext; } close (HL); } sub Event { my ($Obj,$Event,@Args) = @_; if ($Event eq "NewMail" ) { &GetMessage; } elsif ($Event eq "Reminder") { my $appt_item = shift @Args; my $start_time = 'N/A'; my $subject = $appt_item->Subject; my $location = $appt_item->Location; if (defined $appt_item->{Start}){ $start_time = $appt_item->{Start}->Time; } print $socket "APPT: $subject Location: $location Start: $start_ +time\n"; } print "EVENT: $Event\n"; #Win32::OLE->QuitMessageLoop(); } sub GetMessage { my $namespace = $outlook->GetNamespace("MAPI"); my $folder = $namespace->GetDefaultFolder(olFolderInbox); my $mailitems = $folder->Items; my $firstmail = $mailitems->GetFirst; my $subject = $firstmail->Subject; #my $sendername = $firstmail->SenderName; #Outlook security prompt my $sendername = 'unknown'; #print "Sender: '$sendername' Subject: '$subject'\n"; print $socket "MAIL: Sender: '$sendername' Subject: '$subject'\n"; } sub exit_app { $ProcessObj->Kill(0); $mw->destroy; }

Replies are listed 'Best First'.
Re: win32, Outlook OLE & security prompt....
by Errto (Vicar) on Jan 05, 2006 at 02:50 UTC
    Two small suggestions that might help, though both would require largely abandoning the code you've written. One is that instead of using Outlook programmatically, you can connect directly to the Exchange server using the CDO API. I don't know how to do this in current versions, and your Windows box may not have the required libraries, and also it would require you to put your username and password somewhere that your program could find it (or type it into a Tk prompt I guess), but it might be an option. The other thing is that Exchange 2000 and later are configured with IMAP by default, and assuming that's enabled, you can just connect to it from the Linux box using the software of your choice.
      Thanks for the suggestions. Though from my googling research, it seems that whether you talk to the Outlook process via the outlook object model or to the exchange server via CDO, if you try to do it programmatically, the security prompt is triggered. What I wasn't sure of was whether it was possible to enter the username/password combo once in the application, and have it send the authentication each time and event was triggered. I couldn't find any info on something like that...