Programmer's Cookbook

Recipes for the practical programmer

Thursday, February 09, 2006

 

Print Outlook Email using Perl/OLE

This little code snippet is a short example of how Perl can be used to read mails in Outlook using the OLE interface. This particular example looks for a specific mailbox name, then seeks some sub folders by name, then prints out some information about the mails in that folder.

use strict;
use Win32::OLE;

# use existing instance if Outlook is already running, or launce a new one
my $ol;
eval {$ol = Win32::OLE->GetActiveObject('Outlook.Application')};
die "Outlook not installed" if $@;
unless (defined $ol) {
$ol = Win32::OLE->new('Outlook.Application', sub {$_[0]->Quit;})
or die "Oops, cannot start Outlook";
}

my $mailbox = seekFolder($ol->Session, 'Mailbox - Hanson, Robert');
my $inbox = seekFolder($mailbox, 'Inbox');
my $folder = seekFolder($inbox, 'Perl Class');

for (my $i = 1; $i <= $folder->Items->Count; $i++) {
print $folder->Items->Item($i)->SenderName . ":";
print $folder->Items->Item($i)->Subject . "\n";
print $folder->Items->Item($i)->Body . "\n";
}

sub seekFolder {
my $obj = shift;
my $target = shift;

for (my $i = 1; $i <= $obj->Folders->Count; $i++) {
if ( $obj->Folders->Item($i)->Name eq $target ) {
return $obj->Folders->Item($i);
}
}
}

Comments:
Some my friends advised me a tool. I downloaded it and to my great surprise it destroyed my emails. I was upset and entered the I-net. I was lucky - view outlook mail files. As opposed to that tool, it aided my problem for seconds and I knew how it could transforming of all data into thousands of files with *.txt, *.eml and *.vcf files.
 
Hello,

Hope that someone could help me even though this was posted 5 years ago.

I would just like to know how could I get all the keys under Item($i)? I mean how could I print those keys? Because I would like to get the timestamp of the specific email, but I fail to get it.

Any help would be greatly appreciated. Thanks!
 
How do I get the sender Email Address instead of sender name from the code.

Please help to get the address of sender.
 
Does anyone know how to see all of the keys under item($i)?
 
Can anyone please help me to get the email sent time instead of email received time of an email.
 
Thanks for this helpful blog about Outlook mail and I’m lucky to find out this blog because I always need this kind of blog. For any technical support call 0800-090-3220 or visit the website Outlook Contact Number UK
 
Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?