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 responses to “[Ruby] Fetching Emails from Microsoft Outlook”

  1. Puneet Pandey Avatar

    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 Avatar
    Anish

    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. Puneet Pandey Avatar

      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 Avatar
    Anish

    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 Avatar
    Anish

    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 Avatar

    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 Avatar
    Anish

    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.

    1. Puneet Pandey Avatar

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

  7. Anish Avatar
    Anish

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

  8. Anish Avatar
    Anish

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

  9. Ashwin Avatar
    Ashwin

    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 Avatar
    Surendra

    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 comment

I’m Puneet

Namaste! I’m Puneet, and this is my digital space for all things tech. I dive deep into topics like backend engineering, software architecture, and how we build robust systems. I believe in writing simple, practical articles that you can learn from and apply. Let’s build something great together!