KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > MailUserBean


1 /*
2  * @(#)MailUserBean.java 1.6 03/01/10
3  *
4  * Copyright 2001-2003 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * - Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * - Redistribution in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * Neither the name of Sun Microsystems, Inc. or the names of contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * This software is provided "AS IS," without a warranty of any kind. ALL
22  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
23  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
24  * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
25  * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES
26  * SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION
27  * OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
28  * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
29  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
30  * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
31  * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS
32  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33  *
34  * You acknowledge that Software is not designed, licensed or intended
35  * for use in the design, construction, operation or maintenance of any
36  * nuclear facility.
37  *
38  */

39
40 package demo;
41
42 import java.util.*;
43 import javax.mail.*;
44 import javax.naming.*;
45
46 /**
47  * This JavaBean is used to store mail user information.
48  */

49 public class MailUserBean {
50     private Folder folder;
51     private String JavaDoc hostname;
52     private String JavaDoc username;
53     private String JavaDoc password;
54     private Session session;
55     private Store store;
56     private URLName url;
57     private String JavaDoc protocol = "imap";
58     private String JavaDoc mbox = "INBOX";
59
60     public MailUserBean(){}
61
62     /**
63      * Returns the javax.mail.Folder object.
64      */

65     public Folder getFolder() {
66         return folder;
67     }
68     
69     /**
70      * Returns the number of messages in the folder.
71      */

72     public int getMessageCount() throws MessagingException {
73         return folder.getMessageCount();
74     }
75
76     /**
77      * hostname getter method.
78      */

79     public String JavaDoc getHostname() {
80         return hostname;
81     }
82     
83     /**
84      * hostname setter method.
85      */

86     public void setHostname(String JavaDoc hostname) {
87         this.hostname = hostname;
88     }
89     
90     /**
91      * username getter method.
92      */

93     public String JavaDoc getUsername() {
94         return username;
95     }
96
97     /**
98      * username setter method.
99      */

100     public void setUsername(String JavaDoc username) {
101         this.username = username;
102     }
103
104     /**
105      * password getter method.
106      */

107     public String JavaDoc getPassword() {
108         return password;
109     }
110
111     /**
112      * password setter method.
113      */

114     public void setPassword(String JavaDoc password) {
115         this.password = password;
116     }
117
118     /**
119      * session getter method.
120      */

121     public Session getSession() {
122         return session;
123     }
124
125     /**
126      * session setter method.
127      */

128     public void setSession(Session s) {
129         this.session = session;
130     }
131
132     /**
133      * store getter method.
134      */

135     public Store getStore() {
136         return store;
137     }
138
139     /**
140      * store setter method.
141      */

142     public void setStore(Store store) {
143         this.store = store;
144     }
145
146     /**
147      * url getter method.
148      */

149     public URLName getUrl() {
150         return url;
151     }
152
153     /**
154      * Method for checking if the user is logged in.
155      */

156     public boolean isLoggedIn() {
157         return store.isConnected();
158     }
159       
160     /**
161      * Method used to login to the mail host.
162      */

163     public void login() throws Exception JavaDoc {
164         url = new URLName(protocol, getHostname(), -1, mbox,
165                           getUsername(), getPassword());
166     /*
167      * First, try to get the session from JNDI,
168      * as would be done under J2EE.
169      */

170     try {
171         InitialContext ic = new InitialContext();
172         Context ctx = (Context)ic.lookup("java:comp/env");
173         session = (Session)ctx.lookup("MySession");
174     } catch (Exception JavaDoc ex) {
175         // ignore it
176
}
177
178     // if JNDI fails, try the old way that should work everywhere
179
if (session == null) {
180         Properties props = null;
181         try {
182         props = System.getProperties();
183         } catch (SecurityException JavaDoc sex) {
184         props = new Properties();
185         }
186         session = Session.getInstance(props, null);
187     }
188         store = session.getStore(url);
189         store.connect();
190         folder = store.getFolder(url);
191         
192         folder.open(Folder.READ_WRITE);
193     }
194
195     /**
196      * Method used to login to the mail host.
197      */

198     public void login(String JavaDoc hostname, String JavaDoc username, String JavaDoc password)
199         throws Exception JavaDoc {
200             
201         this.hostname = hostname;
202         this.username = username;
203         this.password = password;
204         
205         login();
206     }
207
208     /**
209      * Method used to logout from the mail host.
210      */

211     public void logout() throws MessagingException {
212         folder.close(false);
213         store.close();
214         store = null;
215         session = null;
216     }
217 }
218
219
Popular Tags