Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Win32::OLE & Outlook getting the actual email addresses

by cormanaz (Chaplain)
on Jul 26, 2008 at 19:00 UTC ( #700307=perlquestion: print w/replies, xml ) Need Help??

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

Howdy Bros. I'm working on some Outlook automation using Win32::OLE. Like so:
use Win32::OLE; use Win32::OLE::Variant; use Win32::OLE::Const 'Microsoft Outlook'; my $Outlook = Win32::OLE->GetActiveObject('Outlook.Application'); my $ol = Win32::OLE::Const->Load($Outlook); my $namespace = $Outlook->GetNamespace("MAPI"); my $Folder = $namespace->Folders('mymail')->Folders('Inbox'); my $n = $Folder->Items->Count; for ($i=$n; $i>0; $i--) { my $msg = $Folder->Items($i); my $to = $msg->{'To'}; }

If the message retrieved has a header entry for To like Joe Blow <>, rather than just a raw e-mail address (which is often the case) then $to just contains "Joe Blow". I want the e-mail address (or the full string from the header). Does anyone know how to get it?


Replies are listed 'Best First'.
Re: Win32::OLE & Outlook getting the actual email addresses
by cormanaz (Chaplain) on Jul 26, 2008 at 21:13 UTC
    Well after spending a couple of exciting hours hunting around in the Outlook API references, I think I got this partially figured out.

    If you modify the above code like so:

    my $msg = $Folder->Items($i); my $nrecip = $msg->Recipients->Count; for my $j (1..$nrecip) { print "$j\t",$msg->Recipients($j)->Address,"\t",$msg->Recipients($ +j)->Name,"\n" }
    then you get the addresses and names of the individual recipients.

    I say this is a partial solution because if one of the recipients is someone on your exchange server, you get the exchange ID rather than an SMTP address. If anyone knows how to get the SMTP address instead, please let me know.


      If you can't find a way get those contact's SMTP addresses from Outlook, you could probably look them up in Exchange with Net::LDAP.
Re: Win32::OLE & Outlook getting the actual email addresses
by cormanaz (Chaplain) on Jul 27, 2008 at 18:47 UTC
    Well boys and girls, I spent my Saturday and Sunday morning looking into this problem some more. I sure know how to have a good time, don't I?

    My research indicates that all the cool kids are using an API called Redemption to talk to Outlook. There are lots of good reasons for this that you can read about on their web page. But for the purposes of this thread it gives you an easy (well, OK, not exactly easy but manageable) way to resist Microsoft hegemony by getting SMTP addresses when Outlook decides that what you really should want is one of its X400 addresses.

    So here for your computing enjoyment is some code that uses Redemption to go through your inbox and enumerate the sender and recipients' SMTP addresses.


    #!/usr/bin/perl -w use strict; use Win32::OLE; use Win32::OLE::Variant; use Win32::OLE::Const 'Microsoft Outlook'; # set up Outlook OLE my $Outlook; eval {$Outlook = Win32::OLE->GetActiveObject('Outlook.Application')}; die "Outlook not installed" if $@; unless (defined $Outlook) { $Outlook = Win32::OLE->new('Outlook.Application', sub {$_[0]->Quit +;}) or die "Can't start Outlook: $!"; } my $ol = Win32::OLE::Const->Load($Outlook); my $namespace = $Outlook->GetNamespace("MAPI"); # select mailbox and folder my $mailboxname = "myMailbox"; # change this to the actual name of you +r mailbox my $foldername = 'Inbox'; # or whatever other folder you want to use my $ofolder = $namespace->Folders($mailboxname)->Folders($foldername) +|| die "Can't open folder Inbox: $!\n"; my $sfolder = new Win32::OLE("Redemption.MAPIFolder"); $sfolder->{'Item'} = $ofolder; # process messages my $n = $sfolder->Items->Count; my $sitem = new Win32::OLE("Redemption.SafeMailItem"); for (my $i=$n; $i>0; $i--) { $sitem->{'Item'} = $sfolder->Items($i); print "Subject: ",$sitem->{'Subject'},"\n"; # show sender as insured SMTP address my $sendaddrtype = $sitem->Fields(0xC1E001E); # hex key for SENDER +_ADDRTYPE my $senderemail; if ($sendaddrtype eq 'EX') { $senderemail = $sitem->{'Sender'}->Fields(0x39FE001E); # hex k +ey for EMAIL } else { $senderemail = $sitem->{'SenderEmailAddress'}; } print "\tSender: $senderemail\n"; # show recipients' SMTP addresses my $nrecip = $sitem->Recipients->Count; for my $j (1..$nrecip) { print "\tRecipient $j: ",$sitem->Recipients($j)->AddressEntry- +>SMTPAddress,"\n"; } }

      Hi, How to get the Sender Email Address for the same code with out using the "Redemption.MAPIFolder" module. Thanks in Advance. Regards, Latha Sree

        You lookup the object reference msdn and use the appropriate method (I guess ->Sender)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://700307]
Approved by olus
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2021-04-16 15:10 GMT
Find Nodes?
    Voting Booth?

    No recent polls found