Automating Outlook with Ruby: Inbox & Messages
require 'win32ole' outlook = WIN32OLE.new('Outlook.Application') mapi = outlook.GetNameSpace('MAPI') # Get a reference to the Inbox or other folder: inbox = mapi.GetDefaultFolder(6) personal_folders = mapi.Folders.Item('Personal Folders') baseball_folder = personal_folders.Folders.Item('Baseball') # Get a count of a folder's unread items: puts "#{inbox.UnreadItemCount} unread messages" # Iterate over messages in a folder: inbox.Items.each do |message| # Your code here... end # Retrieve a single message: first_message = inbox.Items(1) # Delete a message: message.Delete # Move a message to another folder: baseball_folder = personal_folders.Folders.Item('Baseball') message.Move(baseball_folder) inbox.Items.Count.downto(1) do |i| message = inbox.Items(i) if message.Subject =~ /cardinals/i message.Move(baseball_folder) end end
Further details and discussion can be found here.