### Eclipse Workspace Patch 1.0
#P james-server-trunk
Index: pop3server/src/test/java/org/apache/james/pop3server/AsyncPOP3ServerTest.java
===================================================================
--- pop3server/src/test/java/org/apache/james/pop3server/AsyncPOP3ServerTest.java	(revision 927116)
+++ pop3server/src/test/java/org/apache/james/pop3server/AsyncPOP3ServerTest.java	(working copy)
@@ -19,16 +19,15 @@
 
 package org.apache.james.pop3server;
 
-import java.io.InputStream;
 import java.io.Reader;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
-import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Date;
 import java.util.List;
 
+import javax.mail.Flags;
 import javax.mail.MessagingException;
-import javax.mail.util.SharedByteArrayInputStream;
 
 import junit.framework.TestCase;
 
@@ -40,19 +39,20 @@
 import org.apache.james.api.dnsservice.DNSService;
 import org.apache.james.api.kernel.mock.FakeLoader;
 import org.apache.james.api.user.UsersRepository;
-import org.apache.james.core.MailImpl;
+import org.apache.james.imap.inmemory.InMemoryMailboxManager;
+import org.apache.james.imap.inmemory.InMemorySubscriptionManager;
+import org.apache.james.imap.mailbox.Mailbox;
+import org.apache.james.imap.mailbox.MailboxSession;
+import org.apache.james.imap.store.Authenticator;
 import org.apache.james.lifecycle.LifecycleUtil;
 import org.apache.james.pop3server.mina.AsyncPOP3Server;
-import org.apache.james.services.MailRepository;
 import org.apache.james.services.MailServer;
 import org.apache.james.socket.ProtocolHandlerChainImpl;
-import org.apache.james.test.mock.james.InMemorySpoolRepository;
 import org.apache.james.test.mock.james.MockFileSystem;
 import org.apache.james.test.mock.james.MockMailServer;
 import org.apache.james.test.util.Util;
 import org.apache.james.userrepository.MockUsersRepository;
 import org.apache.james.util.POP3BeforeSMTPHelper;
-import org.apache.mailet.MailAddress;
 
 public class AsyncPOP3ServerTest extends TestCase {
 
@@ -61,12 +61,11 @@
     private POP3TestConfiguration m_testConfiguration;
     private MockUsersRepository m_usersRepository = new MockUsersRepository();
     private POP3Client m_pop3Protocol = null;
-    private MailImpl testMail1;
-    private MailImpl testMail2;
     private FakeLoader serviceManager;
     private DNSService dnsservice;
     private MockFileSystem fSystem;
     private ProtocolHandlerChainImpl chain;
+    private InMemoryMailboxManager manager;
     
     public AsyncPOP3ServerTest() {
         super("AsyncPOP3ServerTest");
@@ -110,7 +109,17 @@
         serviceManager.put(MailServer.ROLE, m_mailServer);
         serviceManager.put(UsersRepository.ROLE,
                 m_usersRepository);
-       
+        
+        
+        manager = new InMemoryMailboxManager(new Authenticator() {
+            
+            public boolean isAuthentic(String userid, CharSequence passwd) {
+                return m_usersRepository.test(userid, passwd.toString());
+            }
+        }, new InMemorySubscriptionManager());
+        
+        serviceManager.put("mailboxmanager", manager);
+        
         dnsservice = setUpDNSServer();
         serviceManager.put(DNSService.ROLE, setUpDNSServer());
         fSystem = new MockFileSystem();
@@ -133,12 +142,16 @@
     }
     protected void tearDown() throws Exception {
         if (m_pop3Protocol != null) {
-            m_pop3Protocol.sendCommand("quit");
-            m_pop3Protocol.disconnect();
+           if ( m_pop3Protocol.isConnected()){
+               m_pop3Protocol.sendCommand("quit");
+               m_pop3Protocol.disconnect();
+           }
         }
         LifecycleUtil.dispose(m_mailServer);
-        if (testMail1 != null) testMail1.dispose();
-        if (testMail2 != null) testMail2.dispose();
+        
+        manager.deleteEverything();
+        //manager.deleteAll();
+
         super.tearDown();
     }
 
@@ -173,8 +186,6 @@
         m_pop3Protocol.connect("127.0.0.1",m_pop3ListenerPort);
 
         m_usersRepository.addUser("foo", "bar");
-        InMemorySpoolRepository mockMailRepository = new InMemorySpoolRepository();
-        m_mailServer.setUserInbox("foo", mockMailRepository);
 
         // not authenticated
         POP3MessageInfo[] entries = m_pop3Protocol.listMessages();
@@ -194,7 +205,6 @@
         assertEquals(1, m_pop3Protocol.getState());
         assertNull(p3i);
 
-        LifecycleUtil.dispose(mockMailRepository);
     }
 
     // TODO: This currently fails with Async implementation because
@@ -235,8 +245,6 @@
         finishSetUp(m_testConfiguration);
 
         m_usersRepository.addUser("foo", "bar");
-        InMemorySpoolRepository mockMailRepository = new InMemorySpoolRepository();
-        m_mailServer.setUserInbox("foo", mockMailRepository);
 
         m_pop3Protocol = new POP3Client();
         m_pop3Protocol.connect("127.0.0.1",m_pop3ListenerPort);
@@ -250,20 +258,24 @@
         assertEquals("Found unexpected messages", 0, list.length);
 
         m_pop3Protocol.disconnect();
-        
-        setupTestMails(mockMailRepository);
+        String mailboxName = "#mail.foo.INBOX";
+        MailboxSession session = manager.login("foo", "bar", new SimpleLog("Test"));
+        if (manager.mailboxExists(mailboxName, session) == false) {
+            manager.createMailbox(mailboxName, session);
+        }
+        setupTestMails(session,manager.getMailbox(mailboxName, session));
         
         m_pop3Protocol.connect("127.0.0.1",m_pop3ListenerPort);
         m_pop3Protocol.login("foo", "bar");
 
         list = m_pop3Protocol.listUniqueIdentifiers();
         assertEquals("Expected 2 messages, found: "+list.length, 2, list.length);
-        assertEquals("name", list[0].identifier);
-        assertEquals("name2", list[1].identifier);
         
         POP3MessageInfo p3i = m_pop3Protocol.listUniqueIdentifier(1);
         assertNotNull(p3i);
-        assertEquals("name", p3i.identifier);
+        
+        manager.deleteMailbox("#mail.foo.INBOX", session);
+
 
     }
 
@@ -271,9 +283,7 @@
         finishSetUp(m_testConfiguration);
 
         m_usersRepository.addUser("foo", "bar");
-        InMemorySpoolRepository mockMailRepository = new InMemorySpoolRepository();
-        m_mailServer.setUserInbox("foo", mockMailRepository);
-
+        
         m_pop3Protocol = new POP3Client();
         m_pop3Protocol.connect("127.0.0.1",m_pop3ListenerPort);
 
@@ -329,11 +339,15 @@
         m_pop3Protocol.connect("127.0.0.1",m_pop3ListenerPort);
 
         m_usersRepository.addUser("foo2", "bar2");
-        InMemorySpoolRepository mailRep = new InMemorySpoolRepository();
-
-        setupTestMails(mailRep);
-
-        m_mailServer.setUserInbox("foo2", mailRep);
+        
+        String mailboxName = "#mail.foo2.INBOX";
+        MailboxSession session = manager.login("foo2", "bar2", new SimpleLog("Test"));
+        
+        if (manager.mailboxExists(mailboxName, session) == false) {
+            manager.createMailbox(mailboxName, session);
+        }
+        
+        setupTestMails(session,manager.getMailbox(mailboxName, session));
         
         m_pop3Protocol.sendCommand("retr","1");
         assertEquals(0, m_pop3Protocol.getState());
@@ -399,25 +413,18 @@
         Reader r3 = m_pop3Protocol.retrieveMessageTop(entries[0].number, 0);
         assertNotNull(r3);
         r3.close();
-        LifecycleUtil.dispose(mailRep);
+        manager.deleteMailbox(mailboxName, session);
     }
 
-    private void setupTestMails(MailRepository mailRep) throws MessagingException {
-        ArrayList<MailAddress> recipients = new ArrayList<MailAddress>();
-        recipients.add(new MailAddress("recipient@test.com"));
-        InputStream mw = new SharedByteArrayInputStream(
-                                ("Return-path: return@test.com\r\n"+
+    private void setupTestMails(MailboxSession session, Mailbox mailbox) throws MessagingException {
+        byte[] content =        ("Return-path: return@test.com\r\n"+
                                  "Content-Transfer-Encoding: plain\r\n"+
                                  "Subject: test\r\n\r\n"+
-                                 "Body Text POP3ServerTest.setupTestMails\r\n").getBytes());
-        testMail1 = new MailImpl("name", new MailAddress("from@test.com"),
-                        recipients, mw);
-        mailRep.store(testMail1);
-        InputStream mw2 = new SharedByteArrayInputStream(
-                                ("EMPTY").getBytes());
-        testMail2 = new MailImpl("name2", new MailAddress("from@test.com"),
-                                recipients, mw2);
-        mailRep.store(testMail2);
+                                 "Body Text POP3ServerTest.setupTestMails\r\n").getBytes();
+        
+        mailbox.appendMessage(content, new Date(), session, true, new Flags());
+        byte[] content2 = ("EMPTY").getBytes();
+        mailbox.appendMessage(content2, new Date(), session, true, new Flags());
     }
 
     /*
@@ -466,6 +473,7 @@
         }
     }
     */
+    
     public void testIpStored() throws Exception {
         finishSetUp(m_testConfiguration);
 
@@ -474,13 +482,10 @@
 
         String pass = "password";
         m_usersRepository.addUser("foo", pass);
-        InMemorySpoolRepository mockMailRepository = new InMemorySpoolRepository();
-        m_mailServer.setUserInbox("foo", mockMailRepository);
 
         m_pop3Protocol.login("foo", pass);
         assertEquals(1, m_pop3Protocol.getState());
         assertTrue(POP3BeforeSMTPHelper.isAuthorized("127.0.0.1"));
-        LifecycleUtil.dispose(mockMailRepository);
     }
     
     public void testCapa() throws Exception {
@@ -491,8 +496,6 @@
 
          String pass = "password";
          m_usersRepository.addUser("foo", pass);
-         InMemorySpoolRepository mockMailRepository = new InMemorySpoolRepository();
-         m_mailServer.setUserInbox("foo", mockMailRepository);
 
          assertEquals(POP3Reply.OK, m_pop3Protocol.sendCommand("CAPA"));
          
@@ -512,8 +515,6 @@
          assertTrue("contains UIDL", replies.contains("UIDL"));
          assertTrue("contains TOP", replies.contains("TOP"));
 
-         LifecycleUtil.dispose(mockMailRepository);
-
     }
     
 
Index: pop3server/src/main/java/org/apache/james/pop3server/POP3Session.java
===================================================================
--- pop3server/src/main/java/org/apache/james/pop3server/POP3Session.java	(revision 927116)
+++ pop3server/src/main/java/org/apache/james/pop3server/POP3Session.java	(working copy)
@@ -22,11 +22,8 @@
 package org.apache.james.pop3server;
 
 
-import java.util.List;
-
+import org.apache.james.imap.mailbox.Mailbox;
 import org.apache.james.protocols.api.TLSSupportedSession;
-import org.apache.james.services.MailRepository;
-import org.apache.mailet.Mail;
 
 /**
  * All the handlers access this interface to communicate with
@@ -41,6 +38,10 @@
      * deleted from the inbox.
      */
     public final static String DELETED ="DELETED_MAIL";
+    
+    public final static String UID_LIST = "UID_LIST";
+    public final static String DELETED_UID_LIST = "DELETED_UID_LIST";
+    public final static String MAILBOX_SESSION = "MAILBOX_SESSION";
    
     // Authentication states for the POP3 interaction
     /** Waiting for user id */
@@ -76,46 +77,17 @@
     void setHandlerState(int handlerState);
 
     /**
-     * Returns the current user inbox
-     *
-     * @return MailRepository
-     */
-    MailRepository getUserInbox();
-
-    /**
-     * Sets the user's mail repository
-     * 
-     * @param userInbox userInbox
-     */
-    void setUserInbox(MailRepository userInbox);
-
-    /**
      * Returns the mail list contained in the mailbox
      * 
      * @return mailbox content
      */
-    List<Mail> getUserMailbox();
+    Mailbox getUserMailbox();
 
     /**
      * Sets a new mailbox content
      * 
      * @param userMailbox mailbox
      */
-    void setUserMailbox(List<Mail> userMailbox);
-    
-    /**
-     * Returns the backup mailbox
-     * 
-     * @return list backup
-     */
-    List<Mail> getBackupUserMailbox();
-
-
-    /**
-     * Sets a new backup mailbox content
-     * 
-     * @param backupUserMailbox the mailbox backup
-     */
-    void setBackupUserMailbox(List<Mail> backupUserMailbox);
+    void setUserMailbox(Mailbox mailbox);
 }
 
Index: pom.xml
===================================================================
--- pom.xml	(revision 927116)
+++ pom.xml	(working copy)
@@ -382,6 +382,12 @@
     </dependency>
     <dependency>
       <groupId>org.apache.james</groupId>
+      <artifactId>apache-james-imap-memory</artifactId>
+      <version>0.1-M2-SNAPSHOT</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.james</groupId>
       <artifactId>apache-james-imap-mailbox</artifactId>
       <version>0.1-M2-SNAPSHOT</version>
     </dependency>
Index: pop3server/src/main/java/org/apache/james/pop3server/core/ListCmdHandler.java
===================================================================
--- pop3server/src/main/java/org/apache/james/pop3server/core/ListCmdHandler.java	(revision 927116)
+++ pop3server/src/main/java/org/apache/james/pop3server/core/ListCmdHandler.java	(working copy)
@@ -23,16 +23,22 @@
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Iterator;
 import java.util.List;
 
 import javax.mail.MessagingException;
 
+import org.apache.james.imap.mailbox.MailboxException;
+import org.apache.james.imap.mailbox.MailboxSession;
+import org.apache.james.imap.mailbox.MessageRange;
+import org.apache.james.imap.mailbox.MessageResult;
+import org.apache.james.imap.mailbox.MessageResult.FetchGroup;
+import org.apache.james.imap.mailbox.util.FetchGroupImpl;
 import org.apache.james.pop3server.POP3Response;
 import org.apache.james.pop3server.POP3Session;
 import org.apache.james.protocols.api.CommandHandler;
 import org.apache.james.protocols.api.Request;
 import org.apache.james.protocols.api.Response;
-import org.apache.mailet.Mail;
 
 /**
   * Handles LIST command
@@ -52,22 +58,35 @@
     public Response onCommand(POP3Session session, Request request) {
         POP3Response response = null;
         String parameters = request.getArgument();
-        
-        if (session.getHandlerState() == POP3Session.TRANSACTION) {
-            Mail dm = (Mail) session.getState().get(POP3Session.DELETED);
+        List<Long> uidList = (List<Long>) session.getState().get(POP3Session.UID_LIST);
+        List<Long> deletedUidList = (List<Long>) session.getState().get(POP3Session.DELETED_UID_LIST);
 
+        MailboxSession mailboxSession = (MailboxSession) session.getState().get(POP3Session.MAILBOX_SESSION);
+        if (session.getHandlerState() == POP3Session.TRANSACTION) {
             if (parameters == null) {
-
-                long size = 0;
-                int count = 0;
+                
 
                 try {
+                    long size = 0;
+                    int count = 0;
+                	List<MessageResult> validResults = new ArrayList<MessageResult>();
 
-                    for (Mail mc:session.getUserMailbox()) {
-                        if (mc != dm) {
-                            size += mc.getMessageSize();
-                            count++;
-                        }
+                    if (uidList.isEmpty() == false) {
+                    	Iterator<MessageResult> results;
+                    	if (uidList.size() > 1) {
+                    		results =  session.getUserMailbox().getMessages(MessageRange.range(uidList.get(0), uidList.get(uidList.size() -1)), new FetchGroupImpl(FetchGroup.MINIMAL), mailboxSession);
+                    	} else {
+                    		results =  session.getUserMailbox().getMessages(MessageRange.one(uidList.get(0)), new FetchGroupImpl(FetchGroup.MINIMAL), mailboxSession);
+                    	}
+
+                    	while (results.hasNext()) {
+                    		MessageResult result = results.next();
+                    		if (deletedUidList.contains(result.getUid()) == false) {
+                    			size += result.getSize();
+                    			count++;
+                            	validResults.add(result);
+                    		}
+                    	}
                     }
                     StringBuilder responseBuffer =
                         new StringBuilder(32)
@@ -76,32 +95,32 @@
                                 .append(size);
                     response = new POP3Response(POP3Response.OK_RESPONSE, responseBuffer.toString());
                     count = 0;
-                    for (Mail mc:session.getUserMailbox()) {
-                        if (mc != dm) {
-                            responseBuffer =
-                                new StringBuilder(16)
-                                        .append(count)
-                                        .append(" ")
-                                        .append(mc.getMessageSize());
-                            response.appendLine(responseBuffer.toString());
-                        }
-                        count++;
+                    for (int i = 0 ; i < validResults.size(); i++) {
+                        responseBuffer =
+                            new StringBuilder(16)
+                                    .append(i +1 )
+                                    .append(" ")
+                                    .append(validResults.get(i).getSize());
+                        response.appendLine(responseBuffer.toString());
+                        
                     }
                     response.appendLine(".");
-                } catch (MessagingException me) {
+                } catch (MailboxException me) {
                     response = new POP3Response(POP3Response.ERR_RESPONSE);
                 }
             } else {
                 int num = 0;
                 try {
                     num = Integer.parseInt(parameters);
-                    Mail mc = session.getUserMailbox().get(num);
-                    if (mc != dm) {
+                    Long uid = uidList.get(num -1);
+                    if (deletedUidList.contains(uid) != false) {
+                        Iterator<MessageResult> results =  session.getUserMailbox().getMessages(MessageRange.one(uid), new FetchGroupImpl(FetchGroup.MINIMAL), mailboxSession);
+
                         StringBuilder responseBuffer =
                             new StringBuilder(64)
                                     .append(num)
                                     .append(" ")
-                                    .append(mc.getMessageSize());
+                                    .append(results.next().getSize());
                         response = new POP3Response(POP3Response.OK_RESPONSE, responseBuffer.toString());
                     } else {
                         StringBuilder responseBuffer =
Index: pop3server/src/main/java/org/apache/james/pop3server/core/POP3CommandDispatcherLineHandler.java
===================================================================
--- pop3server/src/main/java/org/apache/james/pop3server/core/POP3CommandDispatcherLineHandler.java	(revision 927116)
+++ pop3server/src/main/java/org/apache/james/pop3server/core/POP3CommandDispatcherLineHandler.java	(working copy)
@@ -22,6 +22,10 @@
 import java.util.Arrays;
 import java.util.List;
 
+import javax.annotation.Resource;
+
+import org.apache.james.imap.mailbox.MailboxManager;
+import org.apache.james.imap.mailbox.MailboxSession;
 import org.apache.james.pop3server.POP3Session;
 import org.apache.james.protocols.api.AbstractCommandDispatcher;
 import org.apache.james.protocols.api.CommandHandler;
@@ -35,8 +39,14 @@
         AbstractCommandDispatcher<POP3Session> {
     private final static String[] mandatoryCommands = { "USER", "PASS", "LIST" };
     private final CommandHandler<POP3Session> unknownHandler = new UnknownCmdHandler();
+	private MailboxManager manager;
   
 
+	@Resource(name="mailboxmanager")
+    public void setMailboxManager(MailboxManager manager) {
+    	this.manager = manager;
+    }
+    
 
     /**
      * @see org.apache.james.api.protocol.AbstractCommandDispatcher#getMandatoryCommands()
@@ -59,4 +69,21 @@
         return UnknownCmdHandler.COMMAND_NAME;
     }
 
+	@Override
+	public void onLine(POP3Session session, byte[] line) {
+		MailboxSession mSession = (MailboxSession) session.getState().get(POP3Session.MAILBOX_SESSION);
+		
+		// notify the mailboxmanager about the start of the processing
+		manager.startProcessingRequest(mSession);
+		
+		// do the processing
+		super.onLine(session, line);
+		
+	    // notify the mailboxmanager about the end of the processing
+		manager.endProcessingRequest(mSession);
+		
+	}
+    
+    
+
 }
Index: pop3server/src/main/java/org/apache/james/pop3server/core/PassCmdHandler.java
===================================================================
--- pop3server/src/main/java/org/apache/james/pop3server/core/PassCmdHandler.java	(revision 927116)
+++ pop3server/src/main/java/org/apache/james/pop3server/core/PassCmdHandler.java	(working copy)
@@ -27,13 +27,15 @@
 
 import javax.annotation.Resource;
 
-import org.apache.james.api.user.UsersRepository;
+import org.apache.james.imap.mailbox.BadCredentialsException;
+import org.apache.james.imap.mailbox.Mailbox;
+import org.apache.james.imap.mailbox.MailboxException;
+import org.apache.james.imap.mailbox.MailboxManager;
+import org.apache.james.imap.mailbox.MailboxSession;
 import org.apache.james.pop3server.POP3Response;
 import org.apache.james.pop3server.POP3Session;
 import org.apache.james.protocols.api.Request;
 import org.apache.james.protocols.api.Response;
-import org.apache.james.services.MailRepository;
-import org.apache.james.services.MailServer;
 import org.apache.james.util.POP3BeforeSMTPHelper;
 
 /**
@@ -42,29 +44,14 @@
 public class PassCmdHandler extends RsetCmdHandler {
 
 	private final static String COMMAND_NAME ="PASS";
-	private UsersRepository users;
-	private MailServer mailServer;
+	private MailboxManager mailboxManager;
 
 
-    /**
-     * Sets the users repository.
-     * @param users the users to set
-     */
-    @Resource(name="localusersrepository")
-    public final void setUsers(UsersRepository users) {
-        this.users = users;
-    }
+	@Resource(name="mailboxmanager")
+	public void setMailboxManager(MailboxManager manager) {
+		this.mailboxManager = manager;
+	}
 	
-    /**
-     * Sets the mail server.
-     * @param mailServer the mailServer to set
-     */
-    @Resource(name="James")
-    public final void setMailServer(MailServer mailServer) {
-        this.mailServer = mailServer;
-    }
-    
-    
 	/**
      * Handler method called upon receipt of a PASS command.
      * Reads in and validates the password.
@@ -75,13 +62,24 @@
         POP3Response response = null;
         if (session.getHandlerState() == POP3Session.AUTHENTICATION_USERSET && parameters != null) {
             String passArg = parameters;
-            if (users.test(session.getUser(), passArg)) {
                 try {
-                    MailRepository inbox = mailServer.getUserInbox(session.getUser());
-                    if (inbox == null) {
-                        throw new IllegalStateException("MailServer returned a null inbox for "+session.getUser());
+                    MailboxSession mSession = mailboxManager.login(session.getUser(), passArg, session.getLogger());
+                    StringBuffer sb = new StringBuffer();
+                    sb.append(mailboxManager.getUserNameSpacePrefix());
+                    sb.append(mailboxManager.getDelimiter());
+                    sb.append(session.getUser());
+                    sb.append(mailboxManager.getDelimiter());
+                    sb.append("INBOX");;
+                    String mailboxName = sb.toString();
+                    
+                    // check if mailbox exists.. if not just create it
+                    if (mailboxManager.mailboxExists(mailboxName, mSession) == false) {
+                        mailboxManager.createMailbox(mailboxName, mSession); 
                     }
-                    session.setUserInbox(inbox);
+                    Mailbox mailbox = mailboxManager.getMailbox(mailboxName, mSession);
+                    
+                    session.getState().put(POP3Session.MAILBOX_SESSION, mSession);
+                    session.setUserMailbox(mailbox);
                     stat(session);
                     
                     // Store the ipAddress to use it later for pop before smtp 
@@ -93,7 +91,11 @@
                                 .append(session.getUser());
                     response = new POP3Response(POP3Response.OK_RESPONSE,responseBuffer.toString());
                     session.setHandlerState(POP3Session.TRANSACTION);
-                } catch (RuntimeException e) {
+                } catch (BadCredentialsException e) {
+                	
+                    response = new POP3Response(POP3Response.ERR_RESPONSE, "Authentication failed.");
+                    session.setHandlerState(POP3Session.AUTHENTICATION_READY);
+                } catch (MailboxException e) {
                     session.getLogger().error("Unexpected error accessing mailbox for "+session.getUser(),e);
                     response = new POP3Response(POP3Response.ERR_RESPONSE,"Unexpected error accessing mailbox");
                     session.setHandlerState(POP3Session.AUTHENTICATION_READY);
@@ -103,9 +105,7 @@
 
                 session.setHandlerState(POP3Session.AUTHENTICATION_READY);
             }
-        } else {
-            response = new POP3Response(POP3Response.ERR_RESPONSE);
-        }
+        
         return response;    
     }
 
Index: pop3server/src/main/java/org/apache/james/pop3server/core/QuitCmdHandler.java
===================================================================
--- pop3server/src/main/java/org/apache/james/pop3server/core/QuitCmdHandler.java	(revision 927116)
+++ pop3server/src/main/java/org/apache/james/pop3server/core/QuitCmdHandler.java	(working copy)
@@ -25,21 +25,32 @@
 import java.util.Collection;
 import java.util.List;
 
-import org.apache.commons.collections.ListUtils;
+import javax.annotation.Resource;
+import javax.mail.Flags;
+
+import org.apache.james.imap.mailbox.Mailbox;
+import org.apache.james.imap.mailbox.MailboxException;
+import org.apache.james.imap.mailbox.MailboxManager;
+import org.apache.james.imap.mailbox.MailboxSession;
+import org.apache.james.imap.mailbox.MessageRange;
 import org.apache.james.pop3server.POP3Response;
 import org.apache.james.pop3server.POP3Session;
 import org.apache.james.protocols.api.CommandHandler;
 import org.apache.james.protocols.api.Request;
 import org.apache.james.protocols.api.Response;
-import org.apache.mailet.Mail;
 
 /**
   * Handles QUIT command
   */
 public class QuitCmdHandler implements CommandHandler<POP3Session> {
 	private final static String COMMAND_NAME = "QUIT";
-
+	protected MailboxManager mailboxManager;
 
+	@Resource(name="mailboxmanager")
+	public void setMailboxManager(MailboxManager manager) {
+		this.mailboxManager = manager;
+	}
+	
 	/**
      * Handler method called upon receipt of a QUIT command.
      * This method handles cleanup of the POP3Handler state.
@@ -53,19 +64,29 @@
             response.setEndSession(true);
             return response;
         }
-        List<Mail> toBeRemoved =  ListUtils.subtract(session.getBackupUserMailbox(), session.getUserMailbox());
+        MailboxSession mailboxSession = (MailboxSession) session.getState().get(POP3Session.MAILBOX_SESSION);
+
+        List<Long> toBeRemoved =  (List<Long>) session.getState().get(POP3Session.DELETED_UID_LIST);
         try {
-            session.getUserInbox().remove(toBeRemoved);
-            // for (Iterator it = toBeRemoved.iterator(); it.hasNext(); ) {
-            //    Mail mc = (Mail) it.next();
-            //    userInbox.remove(mc.getName());
-            //}
+            Mailbox mailbox = session.getUserMailbox();
+            
+        	for (int i = 0; i < toBeRemoved.size(); i++) {
+        		MessageRange range = MessageRange.one(toBeRemoved.get(i));
+        		mailbox.setFlags(new Flags(Flags.Flag.DELETED), true, false, range, mailboxSession);
+        		mailbox.expunge(range, mailboxSession);
+        	}
             response = new POP3Response(POP3Response.OK_RESPONSE ,"Apache James POP3 Server signing off.");
         } catch (Exception ex) {
             response = new POP3Response(POP3Response.ERR_RESPONSE,"Some deleted messages were not removed");
-            session.getLogger().error("Some deleted messages were not removed: " + ex.getMessage());
+            session.getLogger().error("Some deleted messages were not removed",ex);
         }
         response.setEndSession(true);
+        try {
+			mailboxManager.logout(mailboxSession, false);
+		} catch (MailboxException e) {
+			// nothing todo on logout
+		}
+        
         return response;    
     }
    
Index: pop3server/pom.xml
===================================================================
--- pop3server/pom.xml	(revision 927116)
+++ pop3server/pom.xml	(working copy)
@@ -54,6 +54,10 @@
     </dependency>
     <dependency>
       <groupId>org.apache.james</groupId>
+      <artifactId>apache-james-imap-mailbox</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.james</groupId>
       <artifactId>james-server-core-api</artifactId>
       <type>test-jar</type>
       <scope>test</scope>
@@ -128,5 +132,10 @@
       <artifactId>slf4j-log4j12</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.apache.james</groupId>
+      <artifactId>apache-james-imap-memory</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 </project>
\ No newline at end of file
Index: pop3server/src/main/java/org/apache/james/pop3server/core/TopCmdHandler.java
===================================================================
--- pop3server/src/main/java/org/apache/james/pop3server/core/TopCmdHandler.java	(revision 927116)
+++ pop3server/src/main/java/org/apache/james/pop3server/core/TopCmdHandler.java	(working copy)
@@ -21,19 +21,29 @@
 
 package org.apache.james.pop3server.core;
 
+import java.io.ByteArrayOutputStream;
+import java.io.FilterOutputStream;
 import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.channels.Channels;
+import java.nio.channels.WritableByteChannel;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Enumeration;
+import java.util.Iterator;
 import java.util.List;
 
 import javax.mail.MessagingException;
 
+import org.apache.james.imap.mailbox.MailboxSession;
+import org.apache.james.imap.mailbox.MessageRange;
+import org.apache.james.imap.mailbox.MessageResult;
+import org.apache.james.imap.mailbox.MessageResult.FetchGroup;
+import org.apache.james.imap.mailbox.MessageResult.Header;
+import org.apache.james.imap.mailbox.util.FetchGroupImpl;
 import org.apache.james.pop3server.POP3Response;
 import org.apache.james.pop3server.POP3Session;
 import org.apache.james.protocols.api.Request;
 import org.apache.james.protocols.api.Response;
-import org.apache.mailet.Mail;
 
 /**
   * Handles TOP command
@@ -71,7 +81,7 @@
 
         if (session.getHandlerState() == POP3Session.TRANSACTION) {
             int num = 0;
-            int lines = 0;
+            int lines = -1;
             try {
                 num = Integer.parseInt(argument);
                 lines = Integer.parseInt(argument1);
@@ -80,20 +90,34 @@
                 return response;
             }
             try {
-                Mail mc = session.getUserMailbox().get(num);
-                Mail dm = (Mail) session.getState().get(POP3Session.DELETED);
+            	List<Long> uidList = (List<Long>) session.getState().get(POP3Session.UID_LIST);
+                List<Long> deletedUidList = (List<Long>) session.getState().get(POP3Session.DELETED_UID_LIST);
+
+                MailboxSession mailboxSession = (MailboxSession) session.getState().get(POP3Session.MAILBOX_SESSION);
+            	Long uid = uidList.get(num -1);
+                if (deletedUidList.contains(uid) == false) {
+                	FetchGroupImpl fetchGroup = new FetchGroupImpl(FetchGroup.BODY_CONTENT);
+                	fetchGroup.or(FetchGroup.HEADERS);
+                	Iterator<MessageResult> results =  session.getUserMailbox().getMessages(MessageRange.one(uid), fetchGroup, mailboxSession);
 
-                if (mc != dm) {
                     response = new POP3Response(POP3Response.OK_RESPONSE, "Message follows");
                     try {
-                        for (Enumeration e = mc.getMessage().getAllHeaderLines(); e.hasMoreElements(); ) {
-                        	response.appendLine(e.nextElement().toString());
-                        }
-                        // add empty line between headers and body
-                        response.appendLine("");
-                        
-                       	writeMessageContentTo(mc, response, lines);
-                       	
+                    	MessageResult result = results.next();
+                    	ByteArrayOutputStream out = new ByteArrayOutputStream();
+                    	WritableByteChannel outChannel = Channels.newChannel(out);
+                    	
+                    	// write headers
+                    	Iterator<Header> headers = result.headers();
+                    	while (headers.hasNext()) {
+                    		headers.next().writeTo(outChannel);
+                    	}
+                    	// headers and body are seperated by a CRLF
+                    	out.write("\r\n".getBytes());
+                    	
+                    	// write body
+                    	result.getBody().writeTo(Channels.newChannel(new CountingBodyOutputStream(out, lines)));
+                    	
+                    	response.appendLine(new String(out.toByteArray()));
                     } finally {
                     	response.appendLine(".");
                     }
@@ -152,4 +176,60 @@
         return commands;
     }
 
+    /**
+     * This OutputStream implementation can be used to limit the body lines which will be written
+     * to the wrapped OutputStream
+     * 
+     * 
+     *
+     */
+    private final class CountingBodyOutputStream extends FilterOutputStream {
+
+    	private int count = 0;
+    	private int limit = -1;
+    	private char lastChar;
+    	
+    	/**
+    	 * 
+    	 * @param out OutputStream to write to
+    	 * @param limit the lines to write to the outputstream. -1 is used for no limits
+    	 */
+		public CountingBodyOutputStream(OutputStream out, int limit) {
+			super(out);
+			this.limit = limit;
+		}
+		
+		@Override
+		public void write(byte[] b, int off, int len) throws IOException {
+			for (int i = off ; i < len; i++) {
+				write(b[i]);
+			}
+		}
+		
+		@Override
+		public void write(byte[] b) throws IOException {
+			for (int i = 0 ; i < b.length; i++) {
+				write(b[i]);
+			}
+		}
+		@Override
+		public void write(int b) throws IOException {
+
+			if (limit != -1) { 
+		        if (count <= limit) {
+		            super.write(b);
+			    }
+		    } else {
+		        super.write(b);
+		    }
+			
+			
+			if (lastChar == '\r' && b == '\n') {
+				count++;
+			}
+			lastChar = (char) b;
+		    
+		}
+    	
+    }
 }
Index: pop3server/src/main/java/org/apache/james/pop3server/mina/POP3SessionImpl.java
===================================================================
--- pop3server/src/main/java/org/apache/james/pop3server/mina/POP3SessionImpl.java	(revision 927116)
+++ pop3server/src/main/java/org/apache/james/pop3server/mina/POP3SessionImpl.java	(working copy)
@@ -26,6 +26,7 @@
 import javax.net.ssl.SSLContext;
 
 import org.apache.commons.logging.Log;
+import org.apache.james.imap.mailbox.Mailbox;
 import org.apache.james.pop3server.POP3HandlerConfigurationData;
 import org.apache.james.pop3server.POP3Session;
 import org.apache.james.services.MailRepository;
@@ -41,13 +42,9 @@
 
 	private Map<String, Object> state = new HashMap<String, Object>();
 
-	private List<Mail> userMailbox;
-
-	private MailRepository userInbox;
-
 	private int handlerState;
 
-	private List<Mail> backupUserMailbox;
+	private Mailbox mailbox;
 	
 	public POP3SessionImpl(POP3HandlerConfigurationData configData, Log logger, IoSession session, SSLContext context) {
 		super(logger, session, context);
@@ -59,13 +56,6 @@
 		this(configData, logger, session, null);
 	}
 	
-	/*
-	 * (non-Javadoc)
-	 * @see org.apache.james.pop3server.POP3Session#getBackupUserMailbox()
-	 */
-	public List<Mail> getBackupUserMailbox() {
-		return backupUserMailbox;
-	}
 
 	/*
 	 * (non-Javadoc)
@@ -91,29 +81,6 @@
 		return state;
 	}
 
-	/*
-	 * (non-Javadoc)
-	 * @see org.apache.james.pop3server.POP3Session#getUserInbox()
-	 */
-	public MailRepository getUserInbox() {
-		return userInbox;
-	}
-
-	/*
-	 * (non-Javadoc)
-	 * @see org.apache.james.pop3server.POP3Session#getUserMailbox()
-	 */
-	public List<Mail> getUserMailbox() {
-		return userMailbox;
-	}
-
-	/*
-	 * (non-Javadoc)
-	 * @see org.apache.james.pop3server.POP3Session#setBackupUserMailbox(java.util.List)
-	 */
-	public void setBackupUserMailbox(List<Mail> backupUserMailbox) {
-		this.backupUserMailbox = backupUserMailbox;
-	}
 
 	/*
 	 * (non-Javadoc)
@@ -125,23 +92,6 @@
 
 	/*
 	 * (non-Javadoc)
-	 * @see org.apache.james.pop3server.POP3Session#setUserInbox(org.apache.james.services.MailRepository)
-	 */
-	public void setUserInbox(MailRepository userInbox) {
-		this.userInbox = userInbox;
-	}
-
-	/*
-	 * (non-Javadoc)
-	 * @see org.apache.james.pop3server.POP3Session#setUserMailbox(java.util.List)
-	 */
-	public void setUserMailbox(List<Mail> userMailbox) {
-		this.userMailbox = userMailbox;
-	}
-
-
-	/*
-	 * (non-Javadoc)
 	 * @see org.apache.james.api.protocol.TLSSupportedSession#resetState()
 	 */
 	public void resetState() {
@@ -150,4 +100,12 @@
 		setHandlerState(AUTHENTICATION_READY);
 	}
 
+	public Mailbox getUserMailbox() {
+		return mailbox;
+	}
+
+	public void setUserMailbox(Mailbox mailbox) {
+		this.mailbox = mailbox;
+	}
+
 }
Index: pop3server/src/main/java/org/apache/james/pop3server/core/RsetCmdHandler.java
===================================================================
--- pop3server/src/main/java/org/apache/james/pop3server/core/RsetCmdHandler.java	(revision 927116)
+++ pop3server/src/main/java/org/apache/james/pop3server/core/RsetCmdHandler.java	(working copy)
@@ -28,12 +28,16 @@
 
 import javax.mail.MessagingException;
 
+import org.apache.james.imap.mailbox.MailboxSession;
+import org.apache.james.imap.mailbox.MessageRange;
+import org.apache.james.imap.mailbox.MessageResult;
+import org.apache.james.imap.mailbox.MessageResult.FetchGroup;
+import org.apache.james.imap.mailbox.util.FetchGroupImpl;
 import org.apache.james.pop3server.POP3Response;
 import org.apache.james.pop3server.POP3Session;
 import org.apache.james.protocols.api.CommandHandler;
 import org.apache.james.protocols.api.Request;
 import org.apache.james.protocols.api.Response;
-import org.apache.mailet.Mail;
 
 
 /**
@@ -41,7 +45,8 @@
   */
 public class RsetCmdHandler implements CommandHandler<POP3Session> {
 	private final static String COMMAND_NAME = "RSET";
-
+	
+	
 	/**
      * Handler method called upon receipt of a RSET command.
      * Calls stat() to reset the mailbox.
@@ -69,31 +74,22 @@
      * user inbox.
      *
      */
-    @SuppressWarnings("unchecked")
     protected void stat(POP3Session session) {
-        ArrayList<Mail> userMailbox = new ArrayList<Mail>();
-        Mail dm = (Mail) session.getState().get(POP3Session.DELETED);
-
-        userMailbox.add(dm);
         try {
-            for (Iterator it = session.getUserInbox().list(); it.hasNext(); ) {
-                String key = (String) it.next();
-                Mail mc = session.getUserInbox().retrieve(key);
-                // Retrieve can return null if the mail is no longer in the store.
-                // In this case we simply continue to the next key
-                if (mc == null) {
-                    continue;
-                }
-                userMailbox.add(mc);
+            MailboxSession mailboxSession = (MailboxSession) session.getState().get(POP3Session.MAILBOX_SESSION);
+
+            List<Long> uids = new ArrayList<Long>();
+        	Iterator<MessageResult> it = session.getUserMailbox().getMessages(MessageRange.all(), new FetchGroupImpl(FetchGroup.MINIMAL), mailboxSession);
+            while (it.hasNext()) {
+            	uids.add(it.next().getUid());
             }
+            session.getState().put(POP3Session.UID_LIST, uids);
+            session.getState().put(POP3Session.DELETED_UID_LIST, new ArrayList<Long>());
         } catch(MessagingException e) {
             // In the event of an exception being thrown there may or may not be anything in userMailbox
             session.getLogger().error("Unable to STAT mail box ", e);
         }
-        finally {
-            session.setUserMailbox(userMailbox);
-            session.setBackupUserMailbox((List<Mail>) userMailbox.clone());
-        }
+        
     }
 
     /**
Index: pop3server/src/main/java/org/apache/james/pop3server/core/RetrCmdHandler.java
===================================================================
--- pop3server/src/main/java/org/apache/james/pop3server/core/RetrCmdHandler.java	(revision 927116)
+++ pop3server/src/main/java/org/apache/james/pop3server/core/RetrCmdHandler.java	(working copy)
@@ -21,6 +21,11 @@
 
 package org.apache.james.pop3server.core;
 
+import org.apache.james.imap.mailbox.MailboxSession;
+import org.apache.james.imap.mailbox.MessageRange;
+import org.apache.james.imap.mailbox.MessageResult;
+import org.apache.james.imap.mailbox.MessageResult.FetchGroup;
+import org.apache.james.imap.mailbox.util.FetchGroupImpl;
 import org.apache.james.pop3server.POP3Response;
 import org.apache.james.pop3server.POP3Session;
 import org.apache.james.protocols.api.CommandHandler;
@@ -32,11 +37,13 @@
 import javax.mail.internet.MimeMessage;
 
 import java.io.BufferedReader;
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.nio.channels.Channels;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Enumeration;
+import java.util.Iterator;
 import java.util.List;
 
 /**
@@ -65,26 +72,26 @@
                 return response;
             }
             try {
-                Mail mc = session.getUserMailbox().get(num);
-                Mail dm = (Mail) session.getState().get(POP3Session.DELETED);
+            	List<Long> uidList = (List<Long>) session.getState().get(POP3Session.UID_LIST);
+                List<Long> deletedUidList = (List<Long>) session.getState().get(POP3Session.DELETED_UID_LIST);
+
+                MailboxSession mailboxSession = (MailboxSession) session.getState().get(POP3Session.MAILBOX_SESSION);
+            	Long uid = uidList.get(num -1);
+                if (deletedUidList.contains(uid) == false) {
+                	Iterator<MessageResult> results =  session.getUserMailbox().getMessages(MessageRange.one(uid), new FetchGroupImpl(FetchGroup.FULL_CONTENT), mailboxSession);
 
-                if (mc != dm) {
                     response = new POP3Response(POP3Response.OK_RESPONSE, "Message follows");
                     try {
-                        for (Enumeration e = mc.getMessage().getAllHeaderLines(); e.hasMoreElements(); ) {
-                            response.appendLine(e.nextElement().toString());
-                        }
-                        // add empty line between headers and body
-                        response.appendLine("");
-                        
-                    	// write the full mail to the client
-                        writeMessageContentTo(mc, response, -1);
-                        
+                    	MessageResult result = results.next();
+                    	ByteArrayOutputStream out = new ByteArrayOutputStream();
+                    	result.getFullContent().writeTo(Channels.newChannel(out));
+                    	
+                    	response.appendLine(new String(out.toByteArray()));
                     } finally {
                     	response.appendLine(".");
-                      
                     }
-                    return response;
+                    
+                	return response;	
                 } else {
                     
                     StringBuilder responseBuffer =
@@ -111,56 +118,6 @@
         }
         return response;
     }
-
-
-    /**
-     * Writes the content of the Mail to the client
-     *
-     * @param mail the Mail to write
-     * @param lines the number of lines to write to the client. If -1 is given it write every line of the given MimeMessage to the client
-     * @param session the POP3Session to use
-     *
-     * @throws MessagingException if the MimeMessage is not set for this Mail
-     * @throws IOException if an error occurs while reading or writing from the stream
-     */
-	protected void writeMessageContentTo(Mail mail,
-			POP3Response response, int lines)
-			throws IOException, MessagingException {
-		String line;
-		BufferedReader br;
-		MimeMessage message = mail.getMessage();
-
-		if (message != null) {
-			br = new BufferedReader(new InputStreamReader(message
-					.getRawInputStream()));
-			try {
-
-				while (lines == -1 || lines > 0) {
-					if ((line = br.readLine()) == null) {
-						break;
-					}
-
-					// add extra dot if line starts with dot
-					if (line.startsWith(".")) {
-						line = "." + line;
-					}
-					response.appendLine(line);
-
-					if (lines != -1) {
-					    // only count down lines if we have so line limit here
-					    lines--;
-					}
-
-				}
-
-			} finally {
-				br.close();
-			}
-		} else {
-			throw new MessagingException("No message set for this Mail!");
-		}
-	}
-	
 	/*
 	 * (non-Javadoc)
 	 * @see org.apache.james.api.protocol.CommonCommandHandler#getImplCommands()
Index: pop3server/src/main/java/org/apache/james/pop3server/core/DeleCmdHandler.java
===================================================================
--- pop3server/src/main/java/org/apache/james/pop3server/core/DeleCmdHandler.java	(revision 927116)
+++ pop3server/src/main/java/org/apache/james/pop3server/core/DeleCmdHandler.java	(working copy)
@@ -26,13 +26,11 @@
 import java.util.List;
 
 
-import org.apache.james.lifecycle.LifecycleUtil;
 import org.apache.james.pop3server.POP3Response;
 import org.apache.james.pop3server.POP3Session;
 import org.apache.james.protocols.api.CommandHandler;
 import org.apache.james.protocols.api.Request;
 import org.apache.james.protocols.api.Response;
-import org.apache.mailet.Mail;
 
 /**
   * Handles DELE command
@@ -57,9 +55,12 @@
                 return response;
             }
             try {
-                Mail mc = session.getUserMailbox().get(num);
-                Mail dm = (Mail) session.getState().get(POP3Session.DELETED);
-                if (mc == dm) {
+                List<Long> uidList = (List<Long>) session.getState().get(POP3Session.UID_LIST);
+                List<Long> deletedUidList = (List<Long>) session.getState().get(POP3Session.DELETED_UID_LIST);
+
+                Long uid = uidList.get(num -1);
+                
+                if (deletedUidList.contains(uid)) {
                     StringBuilder responseBuffer =
                         new StringBuilder(64)
                                 .append("Message (")
@@ -67,10 +68,9 @@
                                 .append(") already deleted.");
                     response = new POP3Response(POP3Response.ERR_RESPONSE,responseBuffer.toString());
                 } else {
-                    session.getUserMailbox().set(num, dm);
+                	deletedUidList.add(uid);
                     // we are replacing our reference with "DELETED", so we have
                     // to dispose the no-more-referenced mail object.
-                    LifecycleUtil.dispose(mc);
                     response = new POP3Response(POP3Response.OK_RESPONSE,"Message deleted");
                 }
             } catch (IndexOutOfBoundsException iob) {
Index: pop3server/src/main/java/org/apache/james/pop3server/core/StatCmdHandler.java
===================================================================
--- pop3server/src/main/java/org/apache/james/pop3server/core/StatCmdHandler.java	(revision 927116)
+++ pop3server/src/main/java/org/apache/james/pop3server/core/StatCmdHandler.java	(working copy)
@@ -23,16 +23,21 @@
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Iterator;
 import java.util.List;
 
 import javax.mail.MessagingException;
 
+import org.apache.james.imap.mailbox.MailboxSession;
+import org.apache.james.imap.mailbox.MessageRange;
+import org.apache.james.imap.mailbox.MessageResult;
+import org.apache.james.imap.mailbox.MessageResult.FetchGroup;
+import org.apache.james.imap.mailbox.util.FetchGroupImpl;
 import org.apache.james.pop3server.POP3Response;
 import org.apache.james.pop3server.POP3Session;
 import org.apache.james.protocols.api.CommandHandler;
 import org.apache.james.protocols.api.Request;
 import org.apache.james.protocols.api.Response;
-import org.apache.mailet.Mail;
 
 /**
   * Handles STAT command
@@ -49,15 +54,23 @@
     public Response onCommand(POP3Session session, Request request) {
         POP3Response response = null;
         if (session.getHandlerState() == POP3Session.TRANSACTION) {
-            long size = 0;
-            int count = 0;
+  
             try {
-                Mail dm = (Mail) session.getState().get(POP3Session.DELETED);
+            	List<Long> uidList = (List<Long>) session.getState().get(POP3Session.UID_LIST);
+                List<Long> deletedUidList = (List<Long>) session.getState().get(POP3Session.DELETED_UID_LIST);
 
-                for (Mail mc: session.getUserMailbox()) {
-                    if (mc != dm) {
-                        size += mc.getMessageSize();
+                MailboxSession mailboxSession = (MailboxSession) session.getState().get(POP3Session.MAILBOX_SESSION);
+            	Iterator<MessageResult> results =  session.getUserMailbox().getMessages(MessageRange.range(uidList.get(0), uidList.get(uidList.size() -1)), new FetchGroupImpl(FetchGroup.MINIMAL), mailboxSession);
+
+                long size = 0;
+                int count = 0;
+            	List<MessageResult> validResults = new ArrayList<MessageResult>();
+                while (results.hasNext()) {
+                	MessageResult result = results.next();
+                    if (deletedUidList.contains(result.getUid()) == false) {
+                        size += result.getSize();
                         count++;
+                        validResults.add(result);
                     }
                 }
                 StringBuilder responseBuffer =
Index: pop3server/src/main/java/org/apache/james/pop3server/core/UidlCmdHandler.java
===================================================================
--- pop3server/src/main/java/org/apache/james/pop3server/core/UidlCmdHandler.java	(revision 927116)
+++ pop3server/src/main/java/org/apache/james/pop3server/core/UidlCmdHandler.java	(working copy)
@@ -30,7 +30,6 @@
 import org.apache.james.protocols.api.CommandHandler;
 import org.apache.james.protocols.api.Request;
 import org.apache.james.protocols.api.Response;
-import org.apache.mailet.Mail;
 
 /**
   * Handles UIDL command
@@ -47,34 +46,36 @@
         POP3Response response = null;
         String parameters = request.getArgument();
         if (session.getHandlerState() == POP3Session.TRANSACTION) {
-            Mail dm = (Mail) session.getState().get(POP3Session.DELETED);
-
+            List<Long> uidList = (List<Long>) session.getState().get(POP3Session.UID_LIST);
+            List<Long> deletedUidList = (List<Long>) session.getState().get(POP3Session.DELETED_UID_LIST);
+              
             if (parameters == null) {
                 response = new POP3Response(POP3Response.OK_RESPONSE,"unique-id listing follows");
-                int count = 0;
-                for (Mail mc:session.getUserMailbox()) {
-                    if (mc != dm) {
+                for (int i = 0; i < uidList.size(); i++) {
+                	Long uid = uidList.get(i);
+                    if (deletedUidList.contains(uid) == false) {
                         StringBuilder responseBuffer =
                             new StringBuilder(64)
-                                    .append(count)
+                                    .append(i + 1 )
                                     .append(" ")
-                                    .append(mc.getName());
-                        response.appendLine(responseBuffer.toString());
+                                    .append(uid);
+                        response.appendLine(responseBuffer.toString());                        
                     }
-                    count++;
                 }
+               
                 response.appendLine(".");
             } else {
                 int num = 0;
                 try {
                     num = Integer.parseInt(parameters);
-                    Mail mc = (Mail) session.getUserMailbox().get(num);
-                    if (mc != dm) {
+                    Long uid = uidList.get(num -1);
+                    if (deletedUidList.contains(uid) == false) {
+ 
                         StringBuilder responseBuffer =
                             new StringBuilder(64)
                                     .append(num)
                                     .append(" ")
-                                    .append(mc.getName());
+                                    .append(uid.hashCode());
                         response = new POP3Response(POP3Response.OK_RESPONSE, responseBuffer.toString());
 
                     } else {
