Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: How to find when a new email arrives in my mailbox (outlook)

by vladdrak (Monk)
on Sep 17, 2004 at 00:28 UTC ( [id://391622]=note: print w/replies, xml ) Need Help??


in reply to How to find when a new email arrives in my mailbox (outlook)

This works for me, season to taste:
use Win32::OLE; use Win32::OLE::Const 'Microsoft Outlook'; my $username="username"; # fix me my $outlook = Win32::OLE->CreateObject("Outlook.Application"); OLEErrChk("Create Outlook Object"); my $namespace=$outlook->GetNameSpace("MAPI"); OLEErrChk("Get MAPI Namespace"); my $ol=Win32::OLE::Const->Load($outlook); OLEErrChk("Loading Outlook Constants"); my $logon=$namespace->Logon($username); OLEErrChk("Logging in"); my $folder=$namespace->Folders(4); OLEErrChk("Opening Folders"); my $inbox=$folder->Folders("Inbox"); OLEErrChk("Get a handle to the Inbox"); my $i=1; while (my $msg=$inbox->Items($i)) { OLEErrChk("Got message id $i"); my @lines=split/\n/,$msg->Body; my $skills=0; my %data=(); LINE: for my $line (@lines) { # do something with the message body.. } my $subject=$msg->Subject; if ($subject =~ /something/) { # blah } $i++; } sub OLEErrChk { my $oleaction=shift; my $errNum=Win32::OLE->LastError; if ($errNum != 0) { die "$oleaction failed: $errNum\n"; } }

Replies are listed 'Best First'.
Re^2: How to find when a new email arrives in my mailbox (outlook)
by dmorgo (Pilgrim) on Sep 17, 2004 at 02:24 UTC
    Can you say a few words about how this is working? I can understand the Perl well enough, but don't know enough about Windows OLE.

    I see there is a place to configure the userid, but no place to configure the server name or password. So I'm guessing this is getting the information by doing all the interaction through Outlook? Cool.

    Also when you do this:

    Win32::OLE->CreateObject("Outlook.Application");

    and then later load the object, are you starting Outlook at that point? Will this still work if Outlook is already started?

    Thanks for posting the code btw.

      I have, I hesitate somewhat to admit, written some similar code myself (hesitation due to general dislike of MSFT APIs), so I will try to answer, though I don't have a Windows box here to verify the code on.

      If Outlook is already running, this code will work without any username or poassword prompts. If not, running the program will cause Outlook to automagically start and present the username/password prompt in its ordinary way.

      The line you quote creates a new instance of an object that one uses, through OLE, to interact with Outlook programmatically. It will indeed start Outlook for you if it is not already running.

      The rest of the code is just the usual code for opening a mailbox in Outlook. The strange method and argument names are simply a reflection of the way MAPI data sources are structured. You can find a VB equivalent of that code fairly easily by searching around the 'net, though Perl is a less common choice for this sort of thing (but I presume it works fine).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://391622]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-20 06:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found