Skip to content

Instantly share code, notes, and snippets.

@karlosmid
Created February 8, 2014 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save karlosmid/8884831 to your computer and use it in GitHub Desktop.
Save karlosmid/8884831 to your computer and use it in GitHub Desktop.
Connect to Microsoft Exchange specific folder using viewpoint gem. You can use owsap proxy if needed.
require 'viewpoint'
class EmailExchange
attr_accessor :folder_name
def initialize username, password, ews_endpoint, exchange_folder_name, proxy
if not (proxy.nil? or proxy.empty?)
Viewpoint::EWS::EWS.set_trust_ca './owasp_zap_root_ca.pem'
end
Viewpoint::EWS::EWS.endpoint = ews_endpoint
Viewpoint::EWS::EWS.set_auth username,password
self.folder_name = exchange_folder_name
end
def get_body_of_inbox_folder_latest_email
inbox = Viewpoint::EWS::Folder.get_folder_by_name(self.folder_name)
if inbox.unread_count == 0
return nil
elsif inbox.unread_count == 1
emails = inbox.todays_items
unread_email = emails.find {|email| email.is_read? == false}
unread_email.mark_read!
return unread_email.body
else
return nil
end
end
def check_mail
check_mail_ten_times = 0
email_body = nil
while check_mail_ten_times <=10 do
check_mail_ten_times = check_mail_ten_times + 1
email_body = get_body_of_inbox_folder_latest_email
if email_body != nil
return email_body
end
end
return email_body
end
def mark_all_as_read
inbox = Viewpoint::EWS::Folder.get_folder_by_name(self.folder_name)
if inbox.unread_count > 0
emails = inbox.todays_items
emails.each {|email| email.mark_read!}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment