Read more

Interacting with a Microsoft Exchange server from Ruby

Henning Koch
August 13, 2014Software engineer at makandra GmbH

Microsoft Exchange service administrators can enable Exchange Web Services Show archive.org snapshot (EWS) which is a rather accessible XML API for interacting with Exchange. This allows you to read and send e-mails, create appointments, invite meeting attendees, track responses, manage to-do tasks, check user availability and all other sorts of things that are usually only accessible from Outlook.

Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

You can implement an EWS by hand-rolling your XML (the docs Show archive.org snapshot always come with both C# and XML examples) or by using a gem like Viewpoint Show archive.org snapshot . Viewpoint has a nice API, but has some bugs and is not feature-complete, so pick your poison.

There is a makandra/viewpoint fork Show archive.org snapshot with some bugs fixed.

Here are some examples for what you can do with EWS, with accompanying Viewpoint code.

Connecting to an exchange server

require 'viewpoint'
include Viewpoint::EWS

endpoint = 'https://your.exchange.server/ews/Exchange.asmx'
user = 'your-user'
pass = 'your-password'

client = Viewpoint::EWSClient.new(endpoint, user, pass)

Listing e-mails from the inbox

inbox = client.get_folder(:inbox)

inbox.items.each do |item|
  puts item.subject
  puts item.body
end

Sending an e-mail

client.send_message(:subject => "Test subject", :body => "Test body", :to_recipients => ['some@person.tld'])

Listing calendar events

calendar = client.get_folder(:calendar)

calendar.items.each do |item|
  p item.subject
  p item.required_attendees
end

Creating an appointment

calendar.create_item(:subject => 'A test meeting', :start => Time.now)

Creating a meeting and invite attendees

Due to a bug in Viewpoint, this won't actually send invitation e-mails. You need to patch the Viewpoint::EWS::Template::CalendarItem class so it sets the :send_meeting_invitations option to "SendToAllAndSaveCopy". Once you have that you can say:

calendar.create_item(:subject => 'A test meeting', :start => Time.now,
  :required_attendees => [
    { :attendee => { :mailbox => { :email_address => 'some@person.tld' } } }
  ]
)

Exchange will update the meeting in your calendar with any responses (Accept / Reject / Tentative). They also appear in the user's inbox as Viewpoint::EWS::Types::MeetingResponse messages.

Posted by Henning Koch to makandra dev (2014-08-13 19:17)