1 56 package org.opencrx.mail.servlet; 57 58 import java.util.Properties ; 59 60 import javax.mail.Flags ; 61 import javax.mail.Folder ; 62 import javax.mail.FolderNotFoundException ; 63 import javax.mail.Message ; 64 import javax.mail.MessagingException ; 65 import javax.mail.NoSuchProviderException ; 66 import javax.mail.Session ; 67 import javax.mail.Store ; 68 import javax.mail.URLName ; 69 import javax.mail.internet.MimeMessage ; 70 71 import org.openmdx.application.log.AppLog; 72 import org.openmdx.base.exception.ServiceException; 73 74 import com.sun.mail.imap.IMAPSSLStore; 75 import com.sun.mail.imap.IMAPStore; 76 import com.sun.mail.pop3.POP3SSLStore; 77 import com.sun.mail.pop3.POP3Store; 78 79 public class MailStore { 80 81 public MailStore( 83 MailServerConfig config 84 ) { 85 super(); 86 this.config = config; 87 } 88 89 96 public void openStore ( 97 ) throws ServiceException { 98 99 AppLog.info("Fetching emails using the following configuration:"); 100 AppLog.info(config.toString()); 101 102 Properties mailProps = new Properties (); 104 mailProps.setProperty("mail.debug", config.debugMode == false ? "false" : "true"); 105 mailProps.setProperty("mail.store.protocol", config.mailProtocol); 106 if(config.isSslRequired) { 107 113 121 mailProps.setProperty( 122 "mail." + config.mailProtocol + ".socketFactory.fallback", 123 "false" 124 ); 125 mailProps.setProperty( 126 "mail." + config.mailProtocol + ".socketFactory.port", 127 "" + this.config.getMailPort() 128 ); 129 } 132 if(IMAP_MODE.equals(config.mailProtocol)) { 133 mailProps.setProperty( 134 "mail.imap.port", 135 "" + this.config.getMailPort() 136 ); 137 } 138 else { 139 mailProps.setProperty( 140 "mail.pop3.port", 141 "" + this.config.getMailPort() 142 ); 143 } 144 Session mailSession = Session.getDefaultInstance(mailProps, null); 145 146 if(config.debugMode) { 148 mailSession.setDebug(true); 149 mailSession.setDebugOut(System.err); 150 } 151 152 URLName url = new URLName ( 155 config.mailProtocol + 156 "://" + 157 config.mailAccount + 158 ":" + 159 config.mailPassword + 160 "@" + 161 config.mailServer + 162 ":" + 163 this.config.getMailPort() 164 ); 165 166 try { 167 if(IMAP_MODE.equalsIgnoreCase(config.mailProtocol)) { 170 this.store = this.config.isSslRequired 171 ? new IMAPSSLStore(mailSession, url) 172 : new IMAPStore(mailSession, url); 173 } 174 else { 175 this.store = config.isSslRequired 176 ? new POP3SSLStore(mailSession, url) 177 : new POP3Store(mailSession, url); 178 } 179 180 if(this.config.getMailPort() == null) { 183 this.store.connect( 184 this.config.getMailServer(), 185 this.config.getMailAccount(), 186 this.config.getMailPassword() 187 ); 188 } 189 else { 190 this.store.connect( 191 this.config.getMailServer(), 192 this.config.getMailPort().intValue(), 193 this.config.getMailAccount(), 194 this.config.getMailPassword() 195 ); 196 } 197 } 198 catch (NoSuchProviderException e) { 199 AppLog.error("Could not establish connection to EMail provider"); 200 ServiceException e0 = new ServiceException(e); 201 AppLog.error(e0.getMessage(), e0.getCause(), 1); 202 throw e0; 203 } 204 catch (MessagingException e) { 205 AppLog.error("Could not establish connection to EMail provider"); 206 ServiceException e0 = new ServiceException(e); 207 AppLog.error(e0.getMessage(), e0.getCause(), 1); 208 throw e0; 209 } 210 } 211 212 219 public SimpleMimeMessage[] getMessages( 220 ) throws ServiceException { 221 SimpleMimeMessage readMsgs[] = new SimpleMimeMessage[]{}; 222 if(this.folder != null) { 223 try { 224 Message [] messages = folder.getMessages(); 225 readMsgs = new SimpleMimeMessage[messages.length]; 226 for(int i = 0; i < messages.length; i++) { 227 Message message = messages[i]; 228 if(message instanceof MimeMessage ) { 229 MimeMessage mimeMsg = (MimeMessage ) message; 230 readMsgs[i] = new SimpleMimeMessage(mimeMsg, true); 231 if(AppLog.isTraceOn()) { 232 AppLog.trace("Read email: " + readMsgs[i].toString()); 233 } 234 } 235 } 236 } 237 catch(NoSuchProviderException e) { 238 AppLog.error("Could not establish connection to EMail provider"); 239 ServiceException e0 = new ServiceException(e); 240 AppLog.error(e0.getMessage(), e0.getCause(), 1); 241 throw e0; 242 } 243 catch(MessagingException e) { 244 AppLog.error("Exception while trying to get messages from folder '" + this.folder.getName() + "'"); 245 ServiceException e0 = new ServiceException(e); 246 AppLog.error(e0.getMessage(), e0.getCause(), 1); 247 throw e0; 248 } 249 } 250 return readMsgs; 251 } 252 253 public void openFolder( 255 String name 256 ) throws ServiceException { 257 try { 258 this.folder = store.getFolder( 259 name == null 260 ? DEFAULT_FOLDERNAME 261 : name 262 ); 263 this.folder.open(Folder.READ_WRITE); 264 } 265 catch(FolderNotFoundException e) { 266 AppLog.error("Could not open the specified folder '" + name + "'"); 267 ServiceException e0 = new ServiceException(e); 268 AppLog.error(e0.getMessage(), e0.getCause(), 1); 269 throw e0; 270 } 271 catch(MessagingException e) { 272 AppLog.error("Exception while opening folder '" + name + "'"); 273 ServiceException e0 = new ServiceException(e); 274 AppLog.error(e0.getMessage(), e0.getCause(), 1); 275 throw e0; 276 } 277 } 278 279 public void closeFolder( 281 ) { 282 if(this.folder != null) { 283 try { 284 this.folder.close(true); 285 } catch(Exception e) {} 286 } 287 } 288 289 294 public void closeStore () { 295 try { 296 store.close(); 297 } catch (MessagingException e) { 298 AppLog.warning("Could not clean up resources"); 299 System.err.println("Could not clean up after importing"); 300 } 301 } 302 303 public static final String POP3_MODE = "pop3"; 307 public static final String IMAP_MODE = "imap"; 308 public static final String DEFAULT_FOLDERNAME = "INBOX"; 309 310 private Store store = null; 311 private Folder folder = null; 312 private MailServerConfig config = null; 313 314 } 315 316 | Popular Tags |