new(host, port_or_options = {}, usessl = false, certs = nil, verify = true)
public
Creates a new Net::IMAP object and connects it to the specified
host.
options is an option hash, each key of which is a symbol.
The available options are:
port |
Port number (default value is 143 for imap, or 993 for imaps)
|
ssl |
If options[:ssl] is true, then an attempt will be made to use SSL (now TLS)
to connect to the server. For this to work OpenSSL [OSSL] and the Ruby OpenSSL [RSSL] extensions need to be installed. If
options[:ssl] is a hash, it’s passed to OpenSSL::SSL::SSLContext#set_params
as parameters.
|
The most common errors are:
Errno::ECONNREFUSED |
Connection refused by host or an intervening firewall.
|
Errno::ETIMEDOUT |
Connection timed out (possibly due to packets being dropped by an
intervening firewall).
|
Errno::ENETUNREACH |
There is no route to that network.
|
SocketError |
Hostname not known or other socket error.
|
Net::IMAP::ByeResponseError |
The connected to the host was successful, but it immediately said goodbye.
|
Show source
def initialize(host, port_or_options = {},
usessl = false, certs = nil, verify = true)
super()
@host = host
begin
options = port_or_options.to_hash
rescue NoMethodError
options = {}
options[:port] = port_or_options
if usessl
options[:ssl] = create_ssl_params(certs, verify)
end
end
@port = options[:port] || (options[:ssl] ? SSL_PORT : PORT)
@tag_prefix = "RUBY"
@tagno = 0
@parser = ResponseParser.new
@sock = TCPSocket.open(@host, @port)
begin
if options[:ssl]
start_tls_session(options[:ssl])
@usessl = true
else
@usessl = false
end
@responses = Hash.new([].freeze)
@tagged_responses = {}
@response_handlers = []
@tagged_response_arrival = new_cond
@continuation_request_arrival = new_cond
@idle_done_cond = nil
@logout_command_tag = nil
@debug_output_bol = true
@exception = nil
@greeting = get_response
if @greeting.nil?
raise Error, "connection closed"
end
if @greeting.name == "BYE"
raise ByeResponseError, @greeting
end
@client_thread = Thread.current
@receiver_thread = Thread.start {
begin
receive_responses
rescue Exception
end
}
@receiver_thread_terminating = false
rescue Exception
@sock.close
raise
end
end