karthik.raju has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, Using Perl Script, How can we read only unread mails from Outlook inbox ? and once we read how to mark that mail as a read ? Please help me on developing this script, this is will be very useful in my current project. Thanks in Advance, Karthik
  • Comment on How to read only unread mail from Outlook Inbox

Replies are listed 'Best First'.
Re: How to read only unread mail from Outlook Inbox
by GrandFather (Saint) on Nov 19, 2014 at 07:23 UTC
Re: How to read only unread mail from Outlook Inbox
by Sathishkumar (Scribe) on Nov 19, 2014 at 07:24 UTC
    Hi, Can you try below code?
    use strict; use warnings; use Win32::OLE; use Win32::OLE::Const 'Microsoft Outlook'; $|++; #what properties do we want to get from Outlook? my @properties = ( "SenderName", "To", "Subject", "Body" ); my $outlook; $outlook = Win32::OLE->new('Outlook.Application'); die unless $outlook; #get the Inbox folder my $namespace = $outlook->GetNamespace("MAPI"); my $folder = $namespace->GetDefaultFolder(6); my $Deleted = $namespace->GetDefaultFolder(3); my $items = $folder->Items; print STDERR "Folder: ", $folder->Name,"\n"; print STDERR "Total entries: ",$items->Count,"\n"; print join(",",@properties),"\n"; print "$items->Count"; my $cnt=$items->Count; for my $itemIndex (1..$items->Count) { my $message = $items->item($itemIndex); next if not defined $message; print " Message :: $message :: $itemIndex--\n"; # $items->item($cnt) my $attach = $message->Attachments(); if ($message->{'Unread'}) { print "Unread EMaail\n"; } next; my @entry; #todo: the following line causes warnings on "Read:" read receipt +messages push @entry, defined($message->{$_}) ? $message->{$_} : 'undef' fo +reach (@properties); if (defined $attach) { #if there's attachments, print out the message info and save t +he attachments print join(",",@entry),"\n"; print "Attachment count == ",$attach->Count,"\n"; for my $attach_index (1..$attach->Count) { my $attachment = $attach->item($attach_index); my $filename = $attachment->Filename; print "Attachment == ",$filename,"\n"; my $saveas = 'E:\Mail'; print "saving attachment '$filename' to '$saveas'...\n"; $attachment->SaveAsFile($saveas); print "$attachment \n"; warn "error saving attachment $filename to $saveas" if !-e + $saveas; } } # $message->Delete; print "Deleted Now \n"; <STDIN>; }
      Hi SatishKumar, Thanks for your response, yes it working fine, but it is not converting the unread mails to read mail. Thanks, Karthik
Re: How to read only unread mail from Outlook Inbox
by Sathishkumar (Scribe) on Nov 21, 2014 at 13:02 UTC
    Hi karthik, The below code convert unread email to read email. $message->{'UnRead'} = 0; Sathish
      Great, working fine. Thanks a lot Satish. -Regards, Karthik

      Hi Satish, what is the bug/error in the below code, actually this is the same code that you have sent, i've done slight modification. This is working fine, and also i'm getting the To() value (mailid) but i'm not getting the mail id From(). use strict; use warnings; use Win32::OLE; use Win32::OLE::Const 'Microsoft Outlook'; my $outlook; $outlook = Win32::OLE->new('Outlook.Application'); die unless $outlook; my $namespace = $outlook->GetNamespace("MAPI"); my $folder = $namespace->GetDefaultFolder(6); my $items = $folder->Items; my $cnt=$items->Count; for my $itemIndex (1..$items->Count) { my $message = $items->item($itemIndex); next if not defined $message; if ($message->{'Unread'}) { my $text = $message->Subject(); my $from_user = $message->From(); my $to_user = $message->To(); print "Unread Email from $from_user and to $to_user\n"; } } I'm getting error message as shown below, why we are not getting the value for From() ? Use of uninitialized value $from_user in concatenation (.) or string at Perl_Rea dUnreadMail.pl line 30. Unread Email from and to xxxxxxxx

        Your code is unreadable. Please put <c> ... </c> tags around your code. See also Markup in the Monastery.

        1 Peter 4:10
Re: How to read only unread mail from Outlook Inbox
by Sathishkumar (Scribe) on Dec 02, 2014 at 10:20 UTC
    Hi Karthik, Improve your knowledge in google search. Do ask help for everything. The following code work perfect.
    use strict; use warnings; use Win32::OLE; use Win32::OLE::Const 'Microsoft Outlook'; # $Win32::OLE::Warn = 3; my $outlook; $outlook = Win32::OLE->new('Outlook.Application'); die unless $outlook; my $namespace = $outlook->GetNamespace("MAPI"); my $folder = $namespace->GetDefaultFolder(6); my $items = $folder->Items; my $cnt=$items->Count; print "cnt :: $cnt \n"; for my $itemIndex (1..$items->Count) { my $message = $items->item($itemIndex); next if not defined $message; if ($message->{'Unread'}) { my $text = $message->Subject(); my $from_user; # $from_user = $from_user.$message->From(); $from_user = $from_user.$message->{'SenderEmailAddress'}; my $to_user = $message->To(); print "Unread Email from $from_user and to $to_user\n"; } }