[Ruby] Fetching Emails from Microsoft Outlook

Hi Folks,

Hope everyone is fine and doing good. In this article of mine, I am going to narrate, how you can fetch Emails from your Microsoft Outlook account. I am only going to cover ‘Inbox‘ here. However, the scope of library (Gem) is very vast and you can customize this as per your need.

PREREQUISITE:

  • Ruby
  • ViewPoint Gem: gem install viewpoint

For Debugging, I am using viewpoint/logging/config

require 'rubygems'
require 'viewpoint' # Library for MS Outlook
require 'viewpoint/logging/config' # For Debugging
include Viewpoint::EWS
endpoint = 'YOUR MS EXACHANGE URL COMES HERE' # E.g. https://OUTLOOKSERVER_URL/ews/exchange.asmx
user = 'USERNAME'
pass = 'PASSWORD'
cli = Viewpoint::EWSClient.new endpoint, user, pass
folders = cli.folders
p folders[0].inspect
inbox = cli.get_folder_by_name 'Inbox'
items = inbox.items
p items[0].body.inspect
p items.count

view raw
outlook.rb
hosted with ❤ by GitHub

Hope this helps someone! Keep in touch and Happy Coding 🙂

Puneet

12 thoughts on “[Ruby] Fetching Emails from Microsoft Outlook

  1. Puneet Pandey May 5, 2015 / 12:36 PM

    If you’re willing to fetch all unread mails, below code snippet will help

    unread_items = inbox.unread_messages
    p items[0].body.inspect

    If you wish to mark all/single unread message to read, below code snippet will do so:

    unread_items[0].mark_read!(read = true)

  2. Anish October 12, 2015 / 1:18 AM

    I tried the code above ,getting the following error : Request URI must have schema. Possibly add ‘http://’ to the request URI?

    at mailbox = cli.get_folder_by_name(‘inbox’).
    Can you please suggest.

    • Puneet Pandey October 12, 2015 / 7:28 AM

      Typically end_point URL should start with HTTP or HTTPS. For e.g.:
      end_point = ‘https://YOUR_EXCHANGE_DOAMIN/excahnge.asmx’
      You can try hitting end_point URL into your browser and in return you’ll receive XML. Let me know, if this helps.

  3. Anish October 12, 2015 / 11:54 PM

    Thanks Punnet for the prompt reply.
    I am using end_point as described by you above and getting an xml response in browser.
    But as I can see in that response there is no method named : get_folder_by_name.
    But there are other methods like GetFolder etc. Can I use these Soap methods to get the details.

    And it’s giving me timeout error at the point : inbox = cli.get_folder_by_name ‘Inbox’
    in the code.

  4. Anish October 13, 2015 / 12:00 AM

    The exact error I am getting is :
    Couldn’t evaluate:cli.get_folder_by_name ‘Inbox’ Unable to read information in the specified timout [20s]

  5. Puneet Pandey October 13, 2015 / 7:36 AM

    Make sure what all folders are there using: folders = cli.folders and let me know, if there is any folder with the name ‘Inbox’ also which version of viewpoint you’re using?

  6. Anish October 14, 2015 / 3:05 AM

    I am using viewpoint (1.0.0) version.
    I am not able to fetch any folders using folders = cli.folders
    I am getting the following error in folders = cli.folders:
    {StandardError} Timeout:evaluation took longer than 10 seconds.

    • Puneet Pandey October 14, 2015 / 10:55 AM

      I am concerned Anish, that you’re able to connect with exchange point! Because looks like this is issue with your endpoint

  7. Anish October 14, 2015 / 3:08 AM

    And my ruby Version is : ruby 2.0.0p481 (2014-05-08) [i386-mingw32]

  8. Anish October 14, 2015 / 3:11 AM

    Can this be a proxy issue. If Yes how can I set the proxy using ViewPoint.

  9. Ashwin March 10, 2016 / 9:00 PM

    Hi I am trying to automate outlook in ruby with viewpoint. How do I find the server address for http://www.live.com or mail.live.com? Can you please help?

  10. Surendra January 20, 2017 / 7:10 PM

    Hi puneeth, I am getting the exception ” Timeout: evaluation took longer than 10 seconds.” when used the command “inbox.items_between startTime, endTime”
    can you help me on this if you have any idea.

    Thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s