Index: /home/pmuir/workspace/connector/src/main/org/jboss/resource/adapter/mail/inflow/IMAPMailFolder.java
===================================================================
--- /home/pmuir/workspace/connector/src/main/org/jboss/resource/adapter/mail/inflow/IMAPMailFolder.java	(revision 0)
+++ /home/pmuir/workspace/connector/src/main/org/jboss/resource/adapter/mail/inflow/IMAPMailFolder.java	(revision 0)
@@ -0,0 +1,50 @@
+package org.jboss.resource.adapter.mail.inflow;
+
+import javax.mail.Folder;
+import javax.mail.Message;
+import javax.mail.MessagingException;
+import javax.mail.NoSuchProviderException;
+import javax.mail.Session;
+import javax.mail.Store;
+import javax.mail.Flags.Flag;
+
+public class IMAPMailFolder extends MailFolder {
+
+	public IMAPMailFolder(MailActivationSpec spec) {
+		super(spec);
+	}
+
+	protected Message[] getMessages(Folder folder) throws MessagingException {
+		if (folder.getUnreadMessageCount() > 0) {
+			int newCount = folder.getUnreadMessageCount();
+			int messageCount = folder.getMessageCount();
+			// folder.getMessages indexes from 1 and uses an inclusive range (ffs)
+			return folder.getMessages(messageCount - newCount + 1, messageCount);
+		}
+		else 
+		{
+			return new Message[0];
+		}
+	}
+
+	protected Store openStore(Session session) throws NoSuchProviderException {
+		return session.getStore("imap");
+	}
+
+	protected void markMessageSeen(Message message) throws MessagingException {
+		message.setFlag(Flag.SEEN, true);
+	}
+
+	protected void closeStore(boolean success, Store store, Folder folder) throws MessagingException {
+		try {
+			if (folder != null && folder.isOpen()) {
+				folder.close(success);
+			}
+		} finally {
+			if (store != null && store.isConnected()) {
+				store.close();
+			}
+		}
+	}
+
+}
Index: /home/pmuir/workspace/connector/src/main/org/jboss/resource/adapter/mail/inflow/MailActivation.java
===================================================================
--- /home/pmuir/workspace/connector/src/main/org/jboss/resource/adapter/mail/inflow/MailActivation.java	(revision 60499)
+++ /home/pmuir/workspace/connector/src/main/org/jboss/resource/adapter/mail/inflow/MailActivation.java	(working copy)
@@ -117,12 +117,11 @@
          log.trace("Begin new msgs check");
       try
       {
-         MailFolder mailFolder = new MailFolder(spec);
+         MailFolder mailFolder = MailFolder.getInstance(spec);
          mailFolder.open();
-         Message[] msgs = mailFolder.getNewMessages();
-         for(int n = 0; released == false && n < msgs.length; n ++)
+         while(mailFolder.hasNext())
          {
-            Message msg = msgs[n];
+            Message msg = (Message) mailFolder.next();
             deliverMsg(msg);
          }
          mailFolder.close();
Index: /home/pmuir/workspace/connector/src/main/org/jboss/resource/adapter/mail/inflow/MailActivationSpec.java
===================================================================
--- /home/pmuir/workspace/connector/src/main/org/jboss/resource/adapter/mail/inflow/MailActivationSpec.java	(revision 60499)
+++ /home/pmuir/workspace/connector/src/main/org/jboss/resource/adapter/mail/inflow/MailActivationSpec.java	(working copy)
@@ -56,6 +56,11 @@
    private long pollingInterval = 60000; 
    /** The maximum number of messages */
    private int maxMessages = 1;
+   /** Enable JavaMail debugging*/
+   private boolean debug;
+   /** flush - for pop3 flush the mailbox after checking */
+   private boolean flush = true;
+   private int port;
 
    public String getMailServer()
    {
@@ -128,6 +133,36 @@
    {
       this.maxMessages = maxMessages;
    }
+   
+   public void setDebug(boolean debug) 
+   {
+	this.debug = debug;
+   }
+   
+   public boolean isDebug() 
+   {
+	return debug;
+   }
+   
+   public int getPort() 
+   {
+	return port;
+   }
+   
+   public void setPort(int port) 
+   {
+	this.port = port;
+   }
+   
+   public void setFlush(boolean flush) 
+   {
+	this.flush = flush;
+   }
+   
+   public boolean isFlush() 
+   {
+	return flush;
+   }
 
    public ResourceAdapter getResourceAdapter()
    {
@@ -161,6 +196,8 @@
       tmp.append(userName);
       tmp.append(", maxMessages=");
       tmp.append(maxMessages);
+      tmp.append(", debug=");
+      tmp.append(debug);
       tmp.append(")");
       return tmp.toString();
    }
Index: /home/pmuir/workspace/connector/src/main/org/jboss/resource/adapter/mail/inflow/MailFolder.java
===================================================================
--- /home/pmuir/workspace/connector/src/main/org/jboss/resource/adapter/mail/inflow/MailFolder.java	(revision 60499)
+++ /home/pmuir/workspace/connector/src/main/org/jboss/resource/adapter/mail/inflow/MailFolder.java	(working copy)
@@ -21,7 +21,10 @@
  */
 package org.jboss.resource.adapter.mail.inflow;
 
+import java.util.Iterator;
 import java.util.Properties;
+
+import javax.mail.NoSuchProviderException;
 import javax.mail.Session;
 import javax.mail.Store;
 import javax.mail.Folder;
@@ -34,7 +37,7 @@
  * @author Scott.Stark@jboss.org
  * @version $Revision$
  */
-public class MailFolder
+public abstract class MailFolder implements Iterator
 {
    private Session session;
    private Store store;
@@ -43,22 +46,30 @@
    private String folderName;
    private String userName;
    private String password;
+   private int port;
+   private boolean debug;
    private Properties sessionProps;
+   
+   private Message[] msgs = {};
+   private int messagePosition;
 
    public MailFolder(MailActivationSpec spec)
-      throws Exception
    {
       mailServer = spec.getMailServer();
-      String protocol = spec.getStoreProtocol();
       folderName = spec.getMailFolder();
       userName = spec.getUserName();
       password = spec.getPassword();
+      debug = spec.isDebug();
+      port = spec.getPort();
 
       sessionProps = new Properties();
       sessionProps.setProperty("mail.transport.protocol", "smtp");
-      sessionProps.setProperty("mail.store.protocol", protocol);
       sessionProps.setProperty("mail.smtp.host", mailServer);
-   }
+      sessionProps.setProperty("mail.debug", spec.isDebug() + "");
+      
+      // JavaMail doesn't implement POP3 STARTTLS
+      sessionProps.setProperty("mail.imap.starttls.enable", "true");
+   }      
 
    public void open()
       throws Exception
@@ -64,10 +75,18 @@
       throws Exception
    {
       // Get a session object
-      session = Session.getDefaultInstance(sessionProps);
+      session = Session.getInstance(sessionProps);
+      session.setDebug(debug);
       // Get a store object
-      store = session.getStore();
-      store.connect(mailServer, userName, password);
+      store = openStore(session);
+      if (port == 0) 
+      {
+    	  store.connect(mailServer, userName, password);
+      }
+      else 
+      {
+    	  store.connect(mailServer, port, userName, password);
+      }
       folder = store.getFolder(folderName);
 
       if (folder == null || (!this.folder.exists()))
@@ -77,33 +96,57 @@
       }
 
       folder.open(Folder.READ_WRITE);
+      msgs = getMessages(folder);
    }
-
-   public Message[] getNewMessages()
-      throws Exception
-   {
-      Message msgs[] = {};
-      /* This does not seem to be the most reliable new msg check. This should
-      probably be unread msgs with the msgs marked as read on successful
-      delivery.
-      */
-      if( folder.hasNewMessages() )
-      {
-         int newCount = folder.getNewMessageCount();
-         int msgCount = folder.getMessageCount();
-         msgs = folder.getMessages(msgCount - newCount + 1, msgCount);
-         return msgs;
-      }
-      return msgs;
-   }
+   
+   protected abstract Store openStore(Session session) throws NoSuchProviderException;
+   
+   protected abstract void closeStore(boolean success, Store store, Folder folder) throws MessagingException;
+   
+   protected abstract Message[] getMessages(Folder folder) throws MessagingException;
+   
+   protected abstract void markMessageSeen(Message message) throws MessagingException;
 
    public void close() throws MessagingException
    {
-      this.folder.close(true);
-      this.store.close();
-      this.folder = null;
-      this.store = null;
-      this.session = null;
+      close(true);
    }
 
+	public boolean hasNext() {
+		return messagePosition < msgs.length; 
+	}
+	
+	public Object next() {
+		try {
+			Message m = msgs[messagePosition++];
+			markMessageSeen(m);
+			return m;
+		} catch (MessagingException e) {
+			close(false);
+			throw new RuntimeException(e);
+		}
+	}
+	
+	public void remove() {
+		throw new UnsupportedOperationException();
+	}
+	
+	protected void close(boolean checkSuccessful) {
+		try {
+			closeStore(checkSuccessful, store, folder);
+		} catch (MessagingException e) {
+			throw new RuntimeException("Error closing mail store", e);
+		}
+	}
+	
+	public static MailFolder getInstance(MailActivationSpec mailActivationSpec) {
+		if ("pop3".equals(mailActivationSpec.getStoreProtocol())) {
+			return new POP3MailFolder(mailActivationSpec);
+		} else if ("imap".equals(mailActivationSpec.getStoreProtocol())) {
+			return new IMAPMailFolder(mailActivationSpec);
+		} else {
+			return null;
+		}
+	}
+
 }
Index: /home/pmuir/workspace/connector/src/main/org/jboss/resource/adapter/mail/inflow/POP3MailFolder.java
===================================================================
--- /home/pmuir/workspace/connector/src/main/org/jboss/resource/adapter/mail/inflow/POP3MailFolder.java	(revision 0)
+++ /home/pmuir/workspace/connector/src/main/org/jboss/resource/adapter/mail/inflow/POP3MailFolder.java	(revision 0)
@@ -0,0 +1,45 @@
+package org.jboss.resource.adapter.mail.inflow;
+
+import javax.mail.Folder;
+import javax.mail.Message;
+import javax.mail.MessagingException;
+import javax.mail.NoSuchProviderException;
+import javax.mail.Session;
+import javax.mail.Store;
+import javax.mail.Flags.Flag;
+
+public class POP3MailFolder extends MailFolder {
+	
+	private boolean flush;
+
+	public POP3MailFolder(MailActivationSpec spec) {
+		super(spec);
+		this.flush = spec.isFlush();
+	}
+
+	protected Message[] getMessages(Folder folder) throws MessagingException {
+		 return folder.getMessages();
+	}
+
+	protected Store openStore(Session session) throws NoSuchProviderException {
+		return session.getStore("pop3");
+	}
+
+	protected void markMessageSeen(Message message) throws MessagingException {
+		message.setFlag(Flag.DELETED, true);
+	}
+
+	protected void closeStore(boolean success, Store store, Folder folder) throws MessagingException {
+		try {
+			if (folder != null && folder.isOpen()) {
+				folder.close(success && flush);
+			}
+		} finally {
+			if (store != null && store.isConnected()) {
+				store.close();
+			}
+		}
+		
+	}
+
+}
