### Installing Net::IMAP Gem Source: https://github.com/ruby/net-imap/blob/master/README.md This section provides instructions for installing the `net-imap` gem. It includes adding the gem to your application's Gemfile and the necessary shell commands to install it. ```Ruby gem 'net-imap' ``` ```Shell bundle install ``` ```Shell gem install net-imap ``` -------------------------------- ### Moving IMAP Messages by Date Range in Ruby Source: https://github.com/ruby/net-imap/blob/master/README.md This example outlines the process of moving specific messages between IMAP mailboxes based on a date range. It selects a source mailbox, conditionally creates a destination mailbox, searches for messages within April 2003, and then either moves them directly or copies and marks them for deletion, followed by an expunge operation. ```Ruby imap.select('Mail/sent-mail') if imap.list('Mail/', 'sent-apr03').empty? imap.create('Mail/sent-apr03') end imap.search(["BEFORE", "30-Apr-2003", "SINCE", "1-Apr-2003"]).each do |message_id| if imap.capable?(:move) || imap.capable?(:IMAP4rev2) imap.move(message_id, "Mail/sent-apr03") else imap.copy(message_id, "Mail/sent-apr03") imap.store(message_id, "+FLAGS", [:Deleted]) end end imap.expunge ``` -------------------------------- ### Connecting and Authenticating with Net::IMAP via TLS Source: https://github.com/ruby/net-imap/blob/master/README.md This code demonstrates establishing a secure TLS connection to an IMAP server on port 993 using `Net::IMAP.new`. It verifies the TLS connection and then proceeds with PLAIN authentication, adapting its behavior based on whether the server's greeting indicates a 'Not Authenticated' or 'Authenticated' state. ```Ruby imap = Net::IMAP.new('mail.example.com', ssl: true) imap.port => 993 imap.tls_verified? => true case imap.greeting.name in /OK/i # The client is connected in the "Not Authenticated" state. imap.authenticate("PLAIN", "joe_user", "joes_password") in /PREAUTH/i # The client is connected in the "Authenticated" state. end ``` -------------------------------- ### Listing Recent Message Sender and Subject in Ruby IMAP Source: https://github.com/ruby/net-imap/blob/master/README.md This snippet shows how to interact with the 'INBOX' mailbox to retrieve details of recent messages. It performs a search for messages marked as 'RECENT', then iterates through each message ID to fetch its 'ENVELOPE' attribute, finally printing the sender's name and the message subject. ```Ruby imap.examine('INBOX') imap.search(["RECENT"]).each do |message_id| envelope = imap.fetch(message_id, "ENVELOPE")[0].attr["ENVELOPE"] puts "#{envelope.from[0].name}: \t#{envelope.subject}" end ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.