http://www.perlmonks.org?node_id=60234


in reply to Accessing MS Exchange

I have grovelled about in Exchange with Win32::OLE, but it's not fun/easy. It really helps to have the VBA documentation for various magic numbers needed.

I never had to do what you want to do, but I did, for instance, dump out all the messages in my folders, which is not possible from the Exchange client.

I offer you the following code, hoping that you may find it a useful starting point.

Disclaimer This may not necessarily be the best way of going about it. It got the job done and then I forgot about the code...

0: #! /usr/perl/bin -w 1: 2: use strict; 3: use Win32::OLE qw/in/; 4: 5: use constant INBOX => 6; 6: 7: my $r; 8: eval { 9: $r = Win32::OLE->GetActiveObject('Outlook.Application') 10: }; 11: if ($@ || !defined($r)) { 12: $r = Win32::OLE->new('Outlook.Application', sub {$_[0]->Quit;} +) or die "oops\n"; 13: } 14: 15: my $namespace = $r->GetNameSpace( 'MAPI' ) or die "can't open +MAPI namespace\n"; 16: 17: my $count = 0; 18: my $folder; 19: 20: if( $folder = $namespace->GetDefaultFolder( INBOX )) { 21: print "$folder->{Name}\n"; 22: my $f = $folder; 23: foreach( @ARGV ) { 24: mkdir $_, 0666; 25: chdir $_; 26: $f = $f->Folders( $_ ); 27: print "$f->{Name}\n" or die Win32::OLE->LastError() . "\n" +; 28: } 29: foreach my $it (reverse in $f->Items) { 30: ++$count; 31: print "$it->{ReceivedTime} $it->{Subject}\n"; 32: my $subj = $it->{Subject}; 33: $subj =~ tr/a-zA-Z0-9/_/c; 34: $subj =~ s/_+/_/g; 35: open OUT, sprintf( ">%03s-$subj.txt", $count ) or die "Can +not open $count for output: $!\n"; 36: print OUT <<EOM; 37: 38: To: $it->{To} 39: CC: $it->{CC} 40: From: $it->{SenderName} 41: Subject: $it->{Subject} 42: Date: $it->{ReceivedTime} 43: 44: $it->{Body} 45: EOM 46: close OUT; 47: } 48: } 49: else { 50: print "nop\n"; 51: }