2

我正在编写一个 Ruby (2.0) 命令行应用程序,该应用程序涉及从用户提供的电子邮件/密码组合发送电子邮件。(为了简化现在的事情,我只使用 Gmail 帐户。)通过使用 Ruby 的 Mail gem,我可以非常轻松地完成电子邮件发送。

Mail gem 似乎也可以在每次发送电子邮件时检查用户电子邮件和密码的真实性。我坚持的是一种无需发送任何邮件即可验证电子邮件/密码的方法。

为了彻底起见,我在下面包含了我的 3 次尝试的输出。没有工作,我无法弄清楚我的错误在哪里。

什么是成功的方法?


目前尝试...

首先我尝试使用 Net::SMTP:

> email = Net::SMTP.new('smtp.gmail.com', 587)
> email.start
> email.starttls
=> #<Net::SMTP::Response:0x007f9ff4133238 @status="220", @string="220 2.0.0 Ready to start TLS\n">

> email.auth_plain(email, password)
EOFError: end of file reached
from /Users/nadams/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/protocol.rb:153:in `read_nonblock'
from /Users/nadams/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/protocol.rb:153:in `rbuf_fill'
from /Users/nadams/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/protocol.rb:134:in `readuntil'
from /Users/nadams/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/protocol.rb:144:in `readline'
from /Users/nadams/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/smtp.rb:931:in `recv_response'
from /Users/nadams/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/smtp.rb:925:in `get_response'
from /Users/nadams/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/smtp.rb:737:in `block in auth_plain'
from /Users/nadams/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/smtp.rb:941:in `critical'
from /Users/nadams/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/smtp.rb:736:in `auth_plain'
from (irb):7
from /Users/nadams/.rvm/rubies/ruby-2.0.0-p0/bin/irb:16:in `<main>'

> email.auth_login(email, password)
NoMethodError: undefined method `success?' for "200 dummy reply code":String
from /Users/nadams/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/smtp.rb:961:in `check_auth_response'
from /Users/nadams/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/smtp.rb:750:in `auth_login'
from (irb):8
from /Users/nadams/.rvm/rubies/ruby-2.0.0-p0/bin/irb:16:in `<main>'

接下来,我尝试使用 Net::POP3:

> email_pop = Net::POP3.new('pop.gmail.com', 995)
=> #<Net::POP3 pop.gmail.com:995 open=false> 
> email_pop.start('dtadams.testmail@gmail.com', 'testmail')
Net::ReadTimeout: Net::ReadTimeout
from /Users/nadams/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/protocol.rb:158:in `rescue in rbuf_fill'
from /Users/nadams/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/protocol.rb:152:in `rbuf_fill'
from /Users/nadams/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/protocol.rb:134:in `readuntil'
from /Users/nadams/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/protocol.rb:144:in `readline'
from /Users/nadams/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/pop.rb:996:in `recv_response'
from /Users/nadams/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/pop.rb:893:in `block in initialize'
from /Users/nadams/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/pop.rb:1012:in `critical'
from /Users/nadams/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/pop.rb:893:in `initialize'
from /Users/nadams/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/pop.rb:562:in `new'
from /Users/nadams/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/pop.rb:562:in `do_start'
from /Users/nadams/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/pop.rb:536:in `start'
from (irb):17
from /Users/nadams/.rvm/rubies/ruby-2.0.0-p0/bin/irb:16:in `<main>'

...最后,对 Net::IMAP 的一次非常不成功的尝试:

> imap = Net::IMAP.new('imap.gmail.com')
Errno::EHOSTUNREACH: No route to host - connect(2)
from /Users/nadams/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/imap.rb:1045:in `initialize'
from /Users/nadams/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/imap.rb:1045:in `open'
from /Users/nadams/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/imap.rb:1045:in `initialize'
from (irb):20:in `new'
from (irb):20
from /Users/nadams/.rvm/rubies/ruby-2.0.0-p0/bin/irb:16:in `<main>'
4

0 回答 0