-----Original Message-----
From: Foo Ji-Haw [mailto:[EMAIL PROTECTED]
Sent: Monday, October 31, 2005 3:13 AM
To: Lincoln, Adym
Cc: [email protected]
Subject: Re: Email with Outlook and Exchange...
Lincoln, Adym wrote:
>Hi all,
>
>Anyone know the best way to read/process email from and
Outlook/Exchange
>server using Perl. I thought it was with Win32::MAPI, but I can't seem
>to locate the module(s). A search on google shows lots of links, but
>they all come up 404'd (Not Found).
>
Try this for a start:
my $outlook = Win32::OLE->new('Outlook.Application');
return undef if (!$outlook);
$MAPI = $outlook->GetNamespace("MAPI");
foreach my $index (1..$MAPI->Folders->Count)
{
print $MAPI->Folders->Item($index)->Name);
}
<snip>
Hi Foo Ji-Haw et al.,
I found an answer...after "finally" locating the Outlook COM
documentation on MSDN online [
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaol11
/html/olobjMAPIFolder_HV05247831.asp ], I was able to locate the
problem...
Turns out, there are (or can be) "multiple" layers of MAPIFolders within
themselves...thus, and 'Outlook Application' can have a collection of
MAPIFolders for each mailbox, and then each mailbox (MAPIFolder) can
have a collection of MAPIFolders (Inbox, Outbox, Drafts, etc...). The
microeye document (
http://www.microeye.com/resources/ObjectModel2003.htm )"does" depict
this relationship, although it is very "subtle"...denote the single
dotted line going back from the MAPIFolder object to the Folders object.
My previous script only touched the first level/layer...MAPIFolder
collection of mailboxes. A finished example script follows for those
interested in the Outlook 2003 COM...Note the two ->Folders collections
in some of the for loops...
<snip>
#!/usr/bin/perl
# LIB : Add local libraries to @INC...
#
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Outlook';
#****************************************************************
# GLOBALS :
#****************************************************************
$gstrCutoffDate = "?"; # global cutoff date string
$gstrEmailUser = "?"; # global userid to access the email
server
$gstrEmailPwd = "?"; # global password to access the email
server
$gblnLogfile = 0; # logfile open indicator...
#****************************************************************
# MAIN : START
#****************************************************************
my $outlook = Win32::OLE->new ( 'Outlook.Application' );
return undef if ( !$outlook );
$MAPI = $outlook->GetNamespace ( "MAPI" );
&printValue ( $MAPI->Folders->Count, "Folders" );
&printLog ( "--------------------------------------------------" );
foreach my $index ( 1..$MAPI->Folders->Count ) {
&printValue ( $MAPI->Folders->Item($index)->Name, "Folder" );
if ( defined $MAPI->Folders->Item($index)->Folders->Count ) {
foreach my $llngIndex (
1..$MAPI->Folders->Item($index)->Folders->Count ) {
&printValue (
$MAPI->Folders->Item($index)->Folders->Item($llngIndex)->Name, "
Inner Folder" );
}
}
}
&printLog ( "--------------------------------------------------\n" );
my $lthInbox;
foreach my $index ( 1..$MAPI->Folders->Count ) {
&printLog ( "--------------------------------------------------" );
&printValue ( $MAPI->Folders->Item($index)->Name, "Folder" );
&printLog ( "--------------------------------------------------" );
if ( $MAPI->Folders->Item($index)->Name eq "Mailbox - Lincoln, Adym"
) {
&printLog ( "FOUND the right Mailbox" );
if ( defined $MAPI->Folders->Item($index)->Folders->Count )
{
foreach my $llngIndex (
1..$MAPI->Folders->Item($index)->Folders->Count ) {
if (
$MAPI->Folders->Item($index)->Folders->Item($llngIndex)->Name eq "Inbox"
) {
$ltheInbox =
$MAPI->Folders->Item($index)->Folders->Item($llngIndex);
&printValue ( $ltheInbox->Name, "Inbox Name" );
&processFolder ( $ltheInbox );
}
}
}
}
}
sub processFolder {
my $ltheFolder = shift @_;
my $ltheItems = $ltheFolder->Items;
&printLog ( " Folder Items" );
&printLog ( " --------------------------------------------------"
);
my $ltheMessage = $ltheItems->GetFirst;
# my $ltheMessage;
# &printValue ( $ltheMessage->Subject, "First Subject" );
while ( defined $ltheMessage ) {
# for my $itemIndex ( 1..$ltheItems->Count ) {
# $ltheMessage = $ltheItems->Item($itemIndex);
# next if not defined $ltheMessage;
&printValue ( $ltheMessage->Subject, "Message Subject" );
$ltheMessage = $ltheItems->GetNext();
}
&printLog ( "
--------------------------------------------------\n\n" );
}
sub printValue() {
my $lstrFunc = "printValue()";
my $lstrValue = $_[0];
my $lstrLabel = defined $_[1] ? $_[1] : "Unknown";
my $llngLabel = length($lstrLabel);
for my $i ( 0.. (30 - $llngLabel) ) {
$lstrLabel = $lstrLabel.".";
}
my $lstrMsg = $lstrLabel."[ ".$lstrValue." ]";
&printLog ( $lstrMsg );
}
sub printLog() {
my $lstrFunc = "printLog()";
my $myMsg = $_[0];
printf "$myMsg\n";
if ( $gblnLogfile ) { printf LOG "$myMsg\n"; }
}
_END_
</snip>
hth,
adym
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs