Topic: Online mailclient

I'm making an online-mailclient with ruby on rails and I'm struggling a little (big) problem

Starting with my code:

def inbox

@mailaccount = Mailaccount.find(params[:id])
begin
imap = Net::IMAP.new( @mailaccount.host, @mailaccount.port)
imap.login(@mailaccount.username, @mailaccount.password)

# selecteer de inbox waarmee je wil werken
imap.select('INBOX')

# alle berichten verkrijgen die niet verwijderd zijn
imap.search(["NOT", "DELETED"]).each do |message_id|

  mail = TMail::Mail.parse(imap.fetch(message_id, "BODY[]")[0].attr["BODY[]"])

    puts "test #{imap.uid_fetch(message_id, 'RFC822')}"

      @name = mail.from.to_s
      @cutname = @name.gsub(/---\n- /,'')
     
      @mail = Inbox.new({:mail_from=>@cutname, :outgoing => false, :subject=>mail.subject, :maildate=>mail.date.strftime("%m/%d/%Y"), :body=>mail.body, :message_number=>mail.message_id, :mailaccount_string => @mailaccount.username, :mailattachment => mail.attachments
        #:name =>mail.from, :to_mail => "contact", :to_company_id => "to_company_id", :company_id => "company_id",
        #:contact_id => "contact_id", :user_id => get_current_user_id
        })
      @mail.save
      if mail.has_attachments?
      mail.attachments.each do |file|
        puts "test#{(@mail.subject)}"
        File.new(RAILS_ROOT+"/documents/mails/"+file.original_filename, "w") { |f|
       
        f.write(file.read)
        }
        #MailAttachment.create({:name => file.original_filename, :extension => file.content_type, :mail_id => @mail.id})
        end
      end
end


    @mails = Inbox.find_all_by_mailaccount_string(@mailaccount.username, :conditions => {:outgoing => false}).sort_by(&:maildate ).reverse
   
imap.expunge
imap.logout
ensure
imap.disconnect
end
render :layout=>false
end

def sent
  #@mailaccount = Mailaccount.find(params[:id])
  begin
imap = Net::IMAP.new( @mailaccount.host, @mailaccount.port)
imap.login(@mailaccount.username, @mailaccount.password)

  # selecteer de inbox waarmee je wil werken
  imap.select('INBOX.Sent')

  # alle berichten verkrijgen die niet verwijderd zijn
  imap.search(["ALL"]).each do |message_id|
    mail = TMail::Mail.parse(imap.fetch(message_id, "BODY[]")[0].attr["BODY[]"])
      @name = mail.to.to_s
      @cutname = @name.gsub(/---\n- /,'')
      Inbox.create({:mail_from=>'exchange@curious.be', :subject=>mail.subject, :maildate=>mail.date.strftime("%m/%d/%Y"), :outgoing => true, :body=>mail.body,:to_mail => @cutname, :message_number=>mail.message_id, :mailaccount_string => 'exchange@curious.be', :mailattachment => mail.attachments
        #:name =>mail.from, :to_mail => "contact", :to_company_id => "to_company_id", :company_id => "company_id",
        #:contact_id => "contact_id", :user_id => get_current_user_id
        })
@mails = Inbox.find_all_by_outgoing(true).sort_by(&:maildate ).reverse
end
imap.expunge
imap.logout
  ensure
    imap.disconnect
  end
      respond_to do |format|
      format.html
      format.js do
      render :partial => false
      end
      end
end

I'm always using the same code to connect with my imap server.
I want to collect inbox messages, sent messages and trash messages through a tree:

INBOX
SENT
TRASH

Does anybody knows a solution to this problem?