Skip to content

Instantly share code, notes, and snippets.

@mribica
Forked from karlosmid/email_exchange.rb
Last active August 29, 2015 14:13
Show Gist options
  • Save mribica/1ae39ddd71f162798c09 to your computer and use it in GitHub Desktop.
Save mribica/1ae39ddd71f162798c09 to your computer and use it in GitHub Desktop.
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