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

Hi, Perl: Activestate 5.6.1, OS Windows 2000, Outlook 200, Exchange 5.5

I'm trying to automate reading email from a public folder and write the results to a webpage. I am able to use
Win32::OLE, Ex: $Outlook=Win32::OLE->new('Outlook.Application');
successfully from the command line, but can't schedule the code, since the Task Scheduler service doesn't load the user registry hive. This is actually a problem for any of the Office products.

I was hoping someone had an alternative way of reading mail from Exchange/Outlook that could also be scheduled.

I have heard of a module that seems promising, but the link to the download has been broken for some time, Win32::MAPI, http://www.generation.net/~aminer/Perl/

It's also not available on CPAN.

Any information, sample code or links would be much appreciated, thank you.

Mitch

edited: Fri Jun 27 16:24:52 2003 by jeffa - s/<br><b>/<br><br>/

Replies are listed 'Best First'.
Re: Mail & Win32
by Foggy Bottoms (Monk) on Jun 27, 2003 at 16:57 UTC
    I believe you can use OLE on it's own - it should be self-sufficient. I assume that your problem is reading the mail as soon as it arrives. Have you had a look at the Inspector object in Outlook ? It could be very useful since it allows you to find different items, then what you could do is have a permanent scan (for example every minute) and check whether new items have come in the inbox (provided there are no rules sending mail to other folders). You can also check whether these items are emails with the following :
    if ( my $outlook = Win32::OLE->GetActiveObject('Outlook.Application')) +# connect to outlook application { # next line for debug only # print "\nSuccessfully connected to existing outlook & email"; + if (defined ($outlook->ActiveInspector())) { my $activeEmail= $outlook->ActiveInspector()->{CurrentItem}; if ($activeEmail->{Class}==olMail) # if we're dealing with an + email item { print "ok it's a mail"; } } }

    This code analyses the currently open item, but it's very easy to make it scan the entire inbox, make it build a hash and each time you scan the inbox you can check to see which mails are new (not unread only new). You can then append these new mails to your hash and use the hash to generate your webpage.
    If you want to run your code at a specific time, why not check the system's time, and compare it to the desired time of execution ?
    Heureux qui, comme Ulysse, a fait un beau voyage
    Ou comme celui-là qui conquit la Toison,
    Et puis est retourné plein d'usage et raison,
    Vivre entre ses parents le reste de son âge!

    J. du Bellay, poëte angevin

      The problem is with automating the job not with the code itself. If I run the job manually it is able to read the mail with no problems. If I run the job manually with a continuous scan there's no problem. However, if I schedule the job or run the code as a service it fails for the reasons described in the article.

      http://support.microsoft.com/default.aspx?scid=kb%3ben-us%3b237913

      I was hoping for another method like Win32::Mapi, but I'm not even sure if that works with Public Folders and I'm having trouble getting the module.

      Mitch
        Is there a cron is Cygwin or PPT? That may be a good idea for this. I'm currently unsure about those offerings.

        Your best option for now may be to have your script run at startup from a run key in the registry or from the startup program group. Then, in order to do what you want every so often, put the code that performs the action in a loop and include a sleep $seconds; statement at the beginning or end of the loop.

        If more accurately spacing the times (depending on various running times of your program) is a priority, you can start the loop like this:
        my $interval = 600; #for ten minutes, adjust as needed my $time_to_work = time() + $interval;
        And end it like this:
        sleep ($time_to_work - time());
        I should point out that this is not pretty even in my mind, but I've used similar code before when running time was variant on input or on network load. You can move the first part (excepting the declarations, of course) to the bottom of the loop and the last part to the top of the loop in order to make it wait before instead of after your main work.

        This means your code is always running, but it's usually taking up (at least very close to) no processor time.

        Christopher E. Stith
Re: Mail & Win32
by gellyfish (Monsignor) on Jun 27, 2003 at 16:04 UTC
    the Task Scheduler service doesn't load the user registry hive.

    One way round this problem is to create a small service that can be run as the appropriate user whose hive is to be loaded and that should fix it. The Service doesn't need to do anything other than start and sit there - you will find example code all over the internet.

    /J\
    
      It didn't work as a service being run as the needed account. I found the following, "The Outlook Object Model is unsuitable for use from an application designed to be run as, or spawned by, an Windows NT service. This includes Active Server Page (ASP) applications running under Internet Information Service (IIS), and applications being run with the AT Scheduler or Task Scheduler services. This is a design limitation of Outlook."

      http://support.microsoft.com/default.aspx?scid=kb%3ben-us%3b237913

      Mitch