1 29 30 package echo2example.email; 31 32 import java.util.Properties ; 33 import java.util.ResourceBundle ; 34 35 import javax.mail.AuthenticationFailedException ; 36 import javax.mail.MessagingException ; 37 import javax.mail.Session ; 38 import javax.mail.Store ; 39 40 import nextapp.echo2.app.ApplicationInstance; 41 import nextapp.echo2.app.Window; 42 43 46 public class EmailApp extends ApplicationInstance { 47 48 53 public static final boolean FAUX_MODE; 54 public static final String MAIL_DOMAIN, RECEIVE_MAIL_SERVER, RECEIVE_PROTOCOL, SEND_MAIL_SERVER, SEND_MAIL_PORT; 55 public static final int MESSAGES_PER_PAGE; 56 57 static { 58 ResourceBundle config = ResourceBundle.getBundle("/echo2example/email/Configuration"); 60 FAUX_MODE = "true".equals(config.getString("FauxMode")); 61 MAIL_DOMAIN = config.getString("MailDomain"); 62 RECEIVE_MAIL_SERVER = config.getString("ReceiveMailServer"); 63 RECEIVE_PROTOCOL = config.getString("ReceiveProtocol"); 64 SEND_MAIL_SERVER = config.getString("SendMailServer"); 65 SEND_MAIL_PORT = config.getString("SendMailPort"); 66 MESSAGES_PER_PAGE = Integer.parseInt(config.getString("MessagesPerPage")); 67 } 68 69 73 private String emailAddress; 74 75 79 private Store store; 80 81 85 private Session mailSession; 86 87 93 public static EmailApp getApp() { 94 return (EmailApp) getActive(); 95 } 96 97 106 public boolean connect(String emailAddress, String password) { 107 Properties properties = System.getProperties(); 108 if (!FAUX_MODE) { 109 properties.put("mail.smtp.host", SEND_MAIL_SERVER); 110 properties.put("mail.smtp.port", SEND_MAIL_PORT); 111 } 112 try { 113 mailSession = Session.getDefaultInstance(properties, null); 114 store = mailSession.getStore(RECEIVE_PROTOCOL); 115 store.connect(RECEIVE_MAIL_SERVER, emailAddress, password); 116 117 this.emailAddress = emailAddress; 119 120 MailScreen mailScreen = new MailScreen(); 122 mailScreen.setStore(store); 123 getDefaultWindow().setContent(mailScreen); 124 } catch (AuthenticationFailedException ex) { 125 return false; 127 } catch (MessagingException ex) { 128 processFatalException(ex); 129 } 130 131 return true; 133 } 134 135 139 public void disconnect() { 140 if (store != null) { 141 try { 142 store.close(); 143 } catch (MessagingException ex) { 144 } 146 store = null; 147 } 148 149 emailAddress = null; 150 getDefaultWindow().setContent(new LoginScreen()); 151 } 152 153 158 public String getEmailAddress() { 159 return emailAddress; 160 } 161 162 167 public Session getMailSession() { 168 return mailSession; 169 } 170 171 174 public Window init() { 175 setStyleSheet(Styles.DEFAULT_STYLE_SHEET); 176 Window window = new Window(); 177 window.setTitle(Messages.getString("Application.Title.Window")); 178 window.setContent(new LoginScreen()); 179 return window; 180 } 181 182 191 public void processFatalException(Exception ex) { 192 ex.printStackTrace(); 193 disconnect(); 194 MessageDialog messageDialog = new MessageDialog(Messages.getString("FatalException.Title"), ex.toString(), 195 MessageDialog.TYPE_ERROR, MessageDialog.CONTROLS_OK); 196 getDefaultWindow().getContent().add(messageDialog); 197 } 198 } 199 | Popular Tags |