Ruby Mechanize Outlook Web Access

发布于 2024-12-14 08:54:35 字数 718 浏览 3 评论 0原文

我正在尝试使用 ruby​​ mechanize 从我的 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

为什么这段代码不起作用?

I am trying to use ruby mechanize to access specific emails from my outlook web access account.

I am using the following code.

    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

Why is this code not working?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一百个冬季 2024-12-21 08:54:35

这是使用 nokogiri 和 mechanize 到 SSL owa 交换网站的 OWA 检索脚本。

它需要安装 ruby​​gems、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.

This is working OWA retrieval script using nokogiri and mechanize to a SSL owa exchange website.

It requires rubygems, mechanize (+deps), and highline to be installed.

    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

Example Script Output:

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

七度光 2024-12-21 08:54:35

我不知道使用目的和问题,但使用 Capybara 和 Selenium 访问该网站可能会更好?

I dont know the purpose of usage and question, but it may be better to use Capybara with Selenium to access this site?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文