2011-11-06 51 views
3

我試圖使用紅寶石機械從我的Outlook Web訪問帳戶訪問特定的電子郵件。Ruby機械化Outlook Web訪問

我正在使用以下代碼。

 
    require 'mechanize' 
    require 'logger' 

    a = Mechanize.new 
    a.cookie_jar(HTTP::Cookies.new) 
    a.log = Logger.new('log1.log') 

    a.get('htts://webmail.xxxxxxx.org/') do |page| 

     my_page = page.form_with(:action => '/owa/auth.owa') do |f| 
     f.username = "------------" 
     f.password = "------------" 
     end.click_button 

     #a.cookie_jar.load('cookies.yml') 

     a.get('https://webmail.xxxxxxx.org/owa/Inbox/?Cmd=contents&Page=1') do |p| 
      file = File.new("new.xml","w+") 
      file.puts p.parser.to_xml 
      file.close 
     end 

    end 

爲什麼這段代碼不工作?

+3

有一個問題在這裏什麼地方? –

回答

3

這是工作OWA檢索腳本使用nokogiri和機械化到SSL owa交換網站。

它需要安裝rubygems,mechanize(+ deps)和highline。

 
    require 'rubygems' 
    require 'mechanize' 
    require 'logger' 
    require 'highline/import' 

    @url = 'https://email.***.***/Exchange' 

    @mechanize = Mechanize.new { |a| a.log = Logger.new('./log1.log') } 

    #In case you dont have trusted certs 
    @mechanize.agent.http.verify_mode = OpenSSL::SSL::VERIFY_NONE 

    @mechanize.user_agent_alias = 'Windows Mozilla' 
    @mechanize.keep_alive = 'enable' 

    username = ask("Enter your username: ") 
    domain = ask("Enter your domain: ") 
    password = ask("Enter your password: ") {|q| q.echo = "*" } 

    @mechanize.get(@url) do |page| 

     form = page.forms[0] 

     form["username"] = domain + '\\' + username 
     form["password"] = password 
     page = form.submit 
    end 

    ## Common Mailbox Schemes 
    @mailbox = "#{username}" 

    # @mailbox = "#{username}@#{domain}.#{tld}" 
    # @mailbox = "#{username}@#{domain}" 

    @inbox = @url + "/#{@mailbox}/Inbox/?Cmd=contents&Page=1&View=Unread%20Messages" 

    inboxlisting = @mechanize.get(@inbox) do |page| 

     fragment = Nokogiri::HTML(page.body) 
     ["//img[@src='/exchweb/img/icon-msg-unread.gif']"].each do |xpathq| 
     puts "Found #{fragment.xpath(xpathq).count} new emails." 
     end 

     ["//img[@src='/exchweb/img/icon-mtgreq.gif']"].each do |xpathq| 
      puts "Found #{fragment.xpath(xpathq).count} new meeting requests." 
     end 

    end 

腳本示例輸出:

 

    $ ruby ./owa.rb 
    Enter your username: john.doe 
    Enter your domain: mywork 
    Enter your password: ************ 
    Found 31 new emails. 
    Found 3 new meeting requests. 

+0

供參考:MIT許可證。版權 - shadowbq 2012 – shadowbq

0

我不知道使用和問題的目的,但它可能會更好地使用硒水豚與硒訪問此網站?