meredib has asked for the wisdom of the Perl Monks concerning the following question:
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); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Send Outlook 2007 email
by zentara (Cardinal) on Nov 11, 2009 at 16:14 UTC | |
by meredib (Acolyte) on Nov 11, 2009 at 16:53 UTC | |
by zentara (Cardinal) on Nov 12, 2009 at 12:27 UTC | |
by meredib (Acolyte) on Nov 12, 2009 at 17:18 UTC | |
by zentara (Cardinal) on Nov 12, 2009 at 17:40 UTC |