class Mail::IMAP

def start(config=Mail::Configuration.instance, &block)

Start an IMAP session and ensures that it will be closed in any case.
def start(config=Mail::Configuration.instance, &block)
  raise ArgumentError.new("Mail::Retrievable#imap_start takes a block") unless block_given?
  if settings[:enable_starttls] && settings[:enable_ssl]
    raise ArgumentError, ":enable_starttls and :enable_ssl are mutually exclusive. Set :enable_ssl if you're on an IMAPS connection. Set :enable_starttls if you're on an IMAP connection and using STARTTLS for secure TLS upgrade."
  end
  imap = Net::IMAP.new(settings[:address], settings[:port], settings[:enable_ssl], nil, false)
  imap.starttls if settings[:enable_starttls]
  if settings[:authentication].nil?
    imap.login(settings[:user_name], settings[:password])
  else
    # Note that Net::IMAP#authenticate('LOGIN', ...) is not equal with Net::IMAP#login(...)!
    # (see also http://www.ensta.fr/~diam/ruby/online/ruby-doc-stdlib/libdoc/net/imap/rdoc/classes/Net/IMAP.html#M000718)
    imap.authenticate(settings[:authentication], settings[:user_name], settings[:password])
  end
  yield imap
ensure
  if defined?(imap) && imap && !imap.disconnected?
    imap.disconnect
  end
end