1 39 40 package demo; 41 42 import java.util.*; 43 import javax.mail.*; 44 import javax.naming.*; 45 46 49 public class MailUserBean { 50 private Folder folder; 51 private String hostname; 52 private String username; 53 private String password; 54 private Session session; 55 private Store store; 56 private URLName url; 57 private String protocol = "imap"; 58 private String mbox = "INBOX"; 59 60 public MailUserBean(){} 61 62 65 public Folder getFolder() { 66 return folder; 67 } 68 69 72 public int getMessageCount() throws MessagingException { 73 return folder.getMessageCount(); 74 } 75 76 79 public String getHostname() { 80 return hostname; 81 } 82 83 86 public void setHostname(String hostname) { 87 this.hostname = hostname; 88 } 89 90 93 public String getUsername() { 94 return username; 95 } 96 97 100 public void setUsername(String username) { 101 this.username = username; 102 } 103 104 107 public String getPassword() { 108 return password; 109 } 110 111 114 public void setPassword(String password) { 115 this.password = password; 116 } 117 118 121 public Session getSession() { 122 return session; 123 } 124 125 128 public void setSession(Session s) { 129 this.session = session; 130 } 131 132 135 public Store getStore() { 136 return store; 137 } 138 139 142 public void setStore(Store store) { 143 this.store = store; 144 } 145 146 149 public URLName getUrl() { 150 return url; 151 } 152 153 156 public boolean isLoggedIn() { 157 return store.isConnected(); 158 } 159 160 163 public void login() throws Exception { 164 url = new URLName(protocol, getHostname(), -1, mbox, 165 getUsername(), getPassword()); 166 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 ex) { 175 } 177 178 if (session == null) { 180 Properties props = null; 181 try { 182 props = System.getProperties(); 183 } catch (SecurityException 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 198 public void login(String hostname, String username, String password) 199 throws Exception { 200 201 this.hostname = hostname; 202 this.username = username; 203 this.password = password; 204 205 login(); 206 } 207 208 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 |