001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.resource.adapter.mail.inflow;
023:
024: import java.util.Properties;
025: import javax.mail.Session;
026: import javax.mail.Store;
027: import javax.mail.Folder;
028: import javax.mail.MessagingException;
029: import javax.mail.Message;
030:
031: /** An encapsulation of a mail store folder used by the MailActivation.run to
032: * poll and retrieve new messages.
033: *
034: * @author Scott.Stark@jboss.org
035: * @version $Revision: 60787 $
036: */
037: public class MailFolder {
038: private Session session;
039: private Store store;
040: private Folder folder;
041: private String mailServer;
042: private String folderName;
043: private String userName;
044: private String password;
045: private Properties sessionProps;
046:
047: public MailFolder(MailActivationSpec spec) throws Exception {
048: mailServer = spec.getMailServer();
049: String protocol = spec.getStoreProtocol();
050: folderName = spec.getMailFolder();
051: userName = spec.getUserName();
052: password = spec.getPassword();
053:
054: sessionProps = new Properties();
055: sessionProps.setProperty("mail.transport.protocol", "smtp");
056: sessionProps.setProperty("mail.store.protocol", protocol);
057: sessionProps.setProperty("mail.smtp.host", mailServer);
058: }
059:
060: public void open() throws Exception {
061: // Get a session object
062: session = Session.getInstance(sessionProps);
063:
064: // Get a store object
065: store = session.getStore();
066: store.connect(mailServer, userName, password);
067: folder = store.getFolder(folderName);
068:
069: if (folder == null || (!this .folder.exists())) {
070: MessagingException e = new MessagingException(
071: "Failed to find folder: " + folderName);
072: throw e;
073: }
074:
075: folder.open(Folder.READ_WRITE);
076: }
077:
078: public Message[] getNewMessages() throws Exception {
079: Message msgs[] = {};
080: /* This does not seem to be the most reliable new msg check. This should
081: probably be unread msgs with the msgs marked as read on successful
082: delivery.
083: */
084: if (folder.hasNewMessages()) {
085: int newCount = folder.getNewMessageCount();
086: int msgCount = folder.getMessageCount();
087: msgs = folder
088: .getMessages(msgCount - newCount + 1, msgCount);
089: return msgs;
090: }
091: return msgs;
092: }
093:
094: public void close() throws MessagingException {
095: this .folder.close(true);
096: this.store.close();
097: this.folder = null;
098: this.store = null;
099: this.session = null;
100: }
101:
102: }
|