I'm trying to send emails via Outlook 2007 after upgrading from Outlook 2003 and am coming across some errors. Below is the code that I have. Currently I have an issue with "$msg = $session->Outbox->Messages->Add();", but I forsee that I may have more issues; for instance "$recipient = $msg->Recipients->Add();". Any help or guidance would be appreciated.
use OLE; use Win32::GUI; use Win32::GuiTest; use Thread; use strict; my ($pwd, $msgFileNm) = @ARGV; shift; shift; open IN, "<".$msgFileNm or die "Can't open file of messages >$msgFileN +m< : $!"; my $session; $session = Win32::OLE->new('Outlook.Application'); die unless $session; #get the Inbox folder my $namespace = $session->GetNamespace("MAPI"); my $folder = $namespace->GetDefaultFolder("olFolderTasks"); # Create a new MAPI session #my $session = Win32::OLE->new("MAPI.Session") or die "Failed to creat +e a MAPI Session: $!"; # Logon to server my $res = $session->Logon('Default Outlook Profile', $pwd);# or die "C +ould not log onto Outlook $!"; my $thread1; print "SendEmailFromFileViaMAPIOutlook.pl ".localtime(time)."\n"; my $msg; my $recipient; my ($to, $sub, $bdy, $cc) = getNextMsg(); while($to gt ''){ $thread1 = Thread->new(\&handleMessageBoxes); # Create a new message $msg = $session->Outbox->Messages->Add(); # Add the recipient and resolve the address $recipient = $msg->Recipients->Add(); $recipient->{Name} = $to; $recipient->{Type} = 1; # 1-to, 2-cc, 3-bcc #$thread1 = Thread->new(\&handleMessageBoxes); $recipient->Resolve(); if(length($cc) > 0){ $recipient = $msg->Recipients->Add(); $recipient->{Name} = $cc; $recipient->{Type} = 2; $recipient->Resolve(); } # Add your text $msg->{Subject} = $sub; #"Email Test"; $msg->{Text} = $bdy; # Send the email $msg->Update(); # 1st argument = save copy of message # 2nd argument = allows user to change message w/dialog box before + sent # 3rd argument = parent window of dialog if 2nd argument is True $msg->Send(0, 0, 0); print "To: $to , cc: $cc Subject: $sub\n"; $thread1->detach(); print "$to\n"; $to = $sub = $bdy = $cc = ''; ($to, $sub, $bdy, $cc) = getNextMsg(); } # Log off $session->Logoff(); sub getNextMsg{ my (@flds, $to, $sub, $bdy, $cc); $to = $sub = $bdy = $cc = ''; while(<IN>){ if($_ =~ /^To:/){ @flds = split(/:/, $_); $to = $flds[1]; chom +p $to; next; } if($_ =~ /^Sub:/){ @flds = split(/:/, $_); $sub = $flds[1]; ch +omp $sub; next; } if($_ =~ /^Cc:/i){ @flds = split(/:/, $_); $cc = $flds[1]; cho +mp $cc; next; } if($_ =~ /^\+\+\+\+\+\+\+\+\+\+/){ last; } $bdy .= $_; } return $to, $sub, $bdy, $cc; } sub handleMessageBoxes{ sleep 1; my $securityHandle = 0; my $cnt = 0; while(not($securityHandle) && ($cnt++ < 10)){ $securityHandle = &waitForSecurityWindow(); } &respondToMessageBox($securityHandle, 't,t,t,e'); sleep 1; my $securityHandle = 0; my $cnt = 0; while(not($securityHandle) && ($cnt++ < 10)){ $securityHandle = &waitForSecurityWindow(); } sleep 6; &respondToMessageBox($securityHandle, 't,t,e'); } sub respondToMessageBox{ my ($hndl, $keySeq) = @_; while($hndl != Win32::GUI::GetForegroundWindow){ Win32::GUI::SetForegroundWindow($hndl); Win32::GUI::Enable($hndl); } my @keys = split(',', $keySeq); foreach my $k (@keys){ my $kval = ($k eq 't') ? "{TAB}" : ($k eq 'e') ? "{ENTER}" : ( +$k eq 's') ? "{SPACE}" : ''; if($kval ne ''){ Win32::GuiTest::SendKeys($kval); } } } sub waitForSecurityWindow{ my $messageClass = "#32770"; my $windowName = 'Microsoft Office Outlook'; return Win32::GUI::FindWindow(undef, $windowName); }

In reply to Send Outlook 2007 email by meredib

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.