KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > adapter > mail > inflow > MailFolder


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.resource.adapter.mail.inflow;
23
24 import java.util.Properties JavaDoc;
25 import javax.mail.Session JavaDoc;
26 import javax.mail.Store JavaDoc;
27 import javax.mail.Folder JavaDoc;
28 import javax.mail.MessagingException JavaDoc;
29 import javax.mail.Message JavaDoc;
30
31 /** An encapsulation of a mail store folder used by the MailActivation.run to
32  * poll and retrieve new messages.
33  *
34  * @author Scott.Stark@jboss.org
35  * @version $Revision: 37459 $
36  */

37 public class MailFolder
38 {
39    private Session JavaDoc session;
40    private Store JavaDoc store;
41    private Folder JavaDoc folder;
42    private String JavaDoc mailServer;
43    private String JavaDoc folderName;
44    private String JavaDoc userName;
45    private String JavaDoc password;
46    private Properties JavaDoc sessionProps;
47
48    public MailFolder(MailActivationSpec spec)
49       throws Exception JavaDoc
50    {
51       mailServer = spec.getMailServer();
52       String JavaDoc protocol = spec.getStoreProtocol();
53       folderName = spec.getMailFolder();
54       userName = spec.getUserName();
55       password = spec.getPassword();
56
57       sessionProps = new Properties JavaDoc();
58       sessionProps.setProperty("mail.transport.protocol", "smtp");
59       sessionProps.setProperty("mail.store.protocol", protocol);
60       sessionProps.setProperty("mail.smtp.host", mailServer);
61    }
62
63    public void open()
64       throws Exception JavaDoc
65    {
66       // Get a session object
67
session = Session.getDefaultInstance(sessionProps);
68       // Get a store object
69
store = session.getStore();
70       store.connect(mailServer, userName, password);
71       folder = store.getFolder(folderName);
72
73       if (folder == null || (!this.folder.exists()))
74       {
75          MessagingException JavaDoc e = new MessagingException JavaDoc("Failed to find folder: " + folderName);
76          throw e;
77       }
78
79       folder.open(Folder.READ_WRITE);
80    }
81
82    public Message JavaDoc[] getNewMessages()
83       throws Exception JavaDoc
84    {
85       Message JavaDoc msgs[] = {};
86       /* This does not seem to be the most reliable new msg check. This should
87       probably be unread msgs with the msgs marked as read on successful
88       delivery.
89       */

90       if( folder.hasNewMessages() )
91       {
92          int newCount = folder.getNewMessageCount();
93          int msgCount = folder.getMessageCount();
94          msgs = folder.getMessages(msgCount - newCount + 1, msgCount);
95          return msgs;
96       }
97       return msgs;
98    }
99
100    public void close() throws MessagingException JavaDoc
101    {
102       this.folder.close(true);
103       this.store.close();
104       this.folder = null;
105       this.store = null;
106       this.session = null;
107    }
108
109 }
110
Popular Tags