当我尝试打开与我的 Office 365 服务的连接时,它失败了:
2.2.0 :001 > require 'net/imap'
=> true
2.2.0 :002 > port = '993'
=> "993"
2.2.0 :003 > imap = Net::IMAP.new('outlook.office365.com', port)
Net::IMAP::Error: connection closed
from /Users/cameronaziz/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/net/imap.rb:1077:in `initialize'
from (irb):3:in `new'
from (irb):3
我还需要设置什么?
这是我 Controller 中的代码:
require 'net/imap'
port = '993'
imap = Net::IMAP.new('outlook.office365.com', port)
imap.authenticate('LOGIN', '***@***.com', '***')
imap.examine('INBOX')
在我实际设置用户名/密码之前,new
命令(与控制台相同)失败。
~~~~~~~
设置 :ssl => true
有效。但是现在我仍然无法连接,因为我得到:
Controller :
imap = Net::IMAP.new('outlook.office365.com', {:port => '993', :ssl => true } )
imap.authenticate('LOGIN', '***@***.com', '***')
View 错误:
Net::IMAP::BadResponseError in EmailsController#connect
Command Argument Error. 11
View 中突出显示的行现在位于 authenticate
方法行上。
当我运行 imap.capability
时:
["IMAP4", "IMAP4REV1", "AUTH=PLAIN", "AUTH=XOAUTH2", "SASL-IR", "UIDPLUS", "ID", "UNSELECT", "CHILDREN", "IDLE", "NAMESPACE", "LITERAL+"]
最佳答案
端口 993 通常是通过 SSL IMAP 连接的方式,因此您需要告诉 ruby 您需要通过 SSL 连接,例如:
imap = Net::IMAP.new('outlook.office365.com', 993, ssl: true)
然后您可以使用登录或身份验证连接到您用户的邮箱。
ruby doc声明如果您使用身份验证方法,您需要确保 imap 服务器可以支持“登录”或“CRAM-MD5”,但在您提供的列表中我没有看到任何这些。
["IMAP4", "IMAP4REV1", "AUTH=PLAIN", "AUTH=XOAUTH2", "SASL-IR", "UIDPLUS", "ID", "UNSELECT", "CHILDREN", "IDLE", “命名空间”,“文字+”]
所以登录outlook imap的唯一方法是使用map.login方法,例如:
imap.login(登录名, 密码
)
关于ruby-on-rails - Net::IMAP 失败:连接已关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31307114/