[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.


  • 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
hosted with ❤ by GitHub

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


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

  1. 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. 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.

    1. 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. 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. 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. 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. 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.

  7. 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.


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