1 32 33 package com.knowgate.hipermail; 34 35 import java.util.Properties ; 36 import java.util.Iterator ; 37 38 import java.security.Security ; 39 40 import javax.mail.AuthenticationFailedException ; 41 import javax.mail.NoSuchProviderException ; 42 import javax.mail.MessagingException ; 43 import javax.mail.internet.ParseException ; 44 import javax.mail.SendFailedException ; 45 import javax.mail.URLName ; 46 import javax.mail.Session ; 47 import javax.mail.Address ; 48 import javax.mail.Message ; 49 import javax.mail.Store ; 50 import javax.mail.Transport ; 51 import javax.mail.Folder ; 52 53 import com.sun.net.ssl.internal.ssl.Provider; 54 55 import com.knowgate.debug.DebugFile; 56 import com.knowgate.dataobjs.DB; 57 58 63 public class SessionHandler { 64 65 private String sInAccountName; 66 private String sInAuthStr; 67 private String sOutAccountName; 68 private String sOutAuthStr; 69 private String sInHostName; 70 private String sOutHostName; 71 private String sMBoxDir; 72 private Properties oProps; 73 private URLName oURLSession; 74 private Session oMailSession; 75 private Session oSmtpSession; 76 private Store oMailStore; 77 private Transport oMailTransport; 78 private boolean bIsStoreConnected; 79 private boolean bIsTransportConnected; 80 private boolean bIncomingSSL; 81 private boolean bOutgoingSSL; 82 83 85 88 public SessionHandler() { 89 bIsStoreConnected = bIsTransportConnected = false; 90 oMailTransport = null; 91 oMailStore = null; 92 oProps = null; 93 sOutAccountName = sOutAuthStr = sInAccountName = sInAuthStr = sInHostName = sOutHostName = sMBoxDir = null; 94 oMailSession = null; 95 bOutgoingSSL = bIncomingSSL=false; 96 } 97 98 100 106 public SessionHandler(MailAccount oAccount) { 107 String sProtocol; 108 oMailStore = null; 109 oMailTransport = null; 110 bIsStoreConnected = bIsTransportConnected = false; 111 sInAccountName=oAccount.getStringNull(DB.incoming_account,null); 112 sInAuthStr=oAccount.getStringNull(DB.incoming_password,null); 113 sOutAccountName=oAccount.getStringNull(DB.outgoing_account,null); 114 sOutAuthStr=oAccount.getStringNull(DB.outgoing_password,null); 115 sInHostName=oAccount.getStringNull(DB.incoming_server,null); 116 sOutHostName=oAccount.getStringNull(DB.outgoing_server,null); 117 if (!oAccount.isNull(DB.incoming_ssl)) 118 bIncomingSSL = (oAccount.getShort(DB.incoming_ssl)==(short)1); 119 if (!oAccount.isNull(DB.outgoing_ssl)) 120 bOutgoingSSL = (oAccount.getShort(DB.outgoing_ssl)==(short)1); 121 122 oProps = new Properties (); 123 124 sProtocol = oAccount.getStringNull(DB.incoming_protocol,"pop3"); 125 oProps.put("mail.store.protocol", sProtocol); 126 oProps.put("mail."+sProtocol+".host", oAccount.getStringNull(DB.incoming_server,"localhost")); 127 if (oAccount.isNull(DB.incoming_port)) 128 oProps.put("mail."+sProtocol+".port", (bIncomingSSL ? "995" : "110")); 129 else 130 oProps.put("mail."+sProtocol+".port", String.valueOf(oAccount.getShort(DB.incoming_port))); 131 if (bIncomingSSL) { 132 oProps.setProperty( "mail."+sProtocol+".socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 133 oProps.setProperty("mail."+sProtocol+".socketFactory.port", oProps.getProperty("mail."+sProtocol+".port")); 134 oProps.setProperty("mail."+sProtocol+".socketFactory.fallback", "false"); 135 } 136 137 sProtocol = oAccount.getStringNull(DB.outgoing_protocol,"smtp"); 138 oProps.put("mail.transport.protocol", sProtocol); 139 oProps.put("mail."+sProtocol+".host", oAccount.getStringNull(DB.outgoing_server,"localhost")); 140 if (oAccount.isNull(DB.outgoing_port)) 141 oProps.put("mail."+sProtocol+".port", (bOutgoingSSL ? "465" : "25")); 142 else 143 oProps.put("mail."+sProtocol+".port", String.valueOf(oAccount.getShort(DB.outgoing_port))); 144 if (bOutgoingSSL) { 145 oProps.setProperty( "mail."+sProtocol+".socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 146 oProps.setProperty("mail."+sProtocol+".socketFactory.port", oProps.getProperty("mail."+sProtocol+".port")); 147 oProps.setProperty("mail."+sProtocol+".socketFactory.fallback", "false"); 148 } 149 150 if (!oAccount.isNull(DB.outgoing_account) && !oAccount.isNull(DB.outgoing_password)) { 151 oProps.put("mail."+sProtocol+".auth", "true"); 152 } 153 154 if (bIncomingSSL || bOutgoingSSL) { 155 Security.addProvider(new Provider()); 156 if (bOutgoingSSL) 157 oProps.put("mail."+oAccount.getStringNull(DB.outgoing_protocol,"smtp")+".starttls.enable","true"); 158 } 159 160 if (DebugFile.trace) { 161 DebugFile.writeln("new SessionHandler(oAccount="+oAccount.getStringNull(DB.gu_account,"null")+")"); 162 DebugFile.incIdent(); 163 Iterator oIter = oProps.keySet().iterator(); 164 while (oIter.hasNext()) { 165 String sKey = (String ) oIter.next(); 166 DebugFile.writeln(sKey+"="+oProps.getProperty(sKey)); 167 } DebugFile.decIdent(); 169 } 170 } 171 172 174 179 public SessionHandler(MailAccount oAccount, String sMBoxDirectory) { 180 this(oAccount); 181 sMBoxDir = sMBoxDirectory; 182 } 183 184 186 191 public String getAccountName() { 192 return sInAccountName; 193 } 194 195 197 public void setAccountName(String aAccName) { 198 sInAccountName=aAccName; 199 } 200 201 203 208 public String getAuthStr() { 209 return sInAuthStr; 210 } 211 212 214 public void setAuthStr(String aAutStr) { 215 sInAuthStr=aAutStr; 216 } 217 218 220 224 public String getHostName() { 225 return sInHostName; 226 } 227 228 230 public void setHostName(String sName) { 231 sInHostName=sName; 232 } 233 234 236 public String getMBoxDirectory() { 237 return sMBoxDir; 238 } 239 240 242 public void setMBoxDirectory(String sDir) { 243 sMBoxDir=sDir; 244 } 245 246 248 public Properties getProperties() { 249 return oProps; 250 } 251 252 254 public void setProperties(Properties oPropties) { 255 oProps=oPropties; 256 } 257 258 260 269 public Session getSession() throws IllegalStateException { 270 if (DebugFile.trace) { 271 DebugFile.writeln("Begin SessionHandler.getSession()"); 272 DebugFile.incIdent(); 273 } 274 if (null==oMailSession) { 275 if (null==oProps) { 276 if (DebugFile.trace) DebugFile.decIdent(); 277 throw new IllegalStateException ("SessionHandler properties not set"); 278 } 279 if (null==sInAccountName) { 280 if (DebugFile.trace) DebugFile.decIdent(); 281 throw new NullPointerException ("SessionHandler account name not set"); 282 } 283 if (DebugFile.trace) DebugFile.writeln("new SilentAuthenticator("+sInAccountName+", ...)"); 284 SilentAuthenticator oAuth = new SilentAuthenticator(sInAccountName, sInAuthStr); 285 if (DebugFile.trace) DebugFile.writeln("Session.getInstance([Properties],[SilentAuthenticator])"); 286 oMailSession = Session.getInstance(oProps, oAuth); 287 } 288 if (DebugFile.trace) { 289 DebugFile.decIdent(); 290 DebugFile.writeln("End SessionHandler.getSession() : " + oMailSession); 291 } 292 return oMailSession; 293 } 295 297 306 public Session getSmtpSession() throws IllegalStateException { 307 if (DebugFile.trace) { 308 DebugFile.writeln("Begin SessionHandler.getSmtpSession()"); 309 DebugFile.incIdent(); 310 } 311 if (null==oSmtpSession) { 312 if (null==oProps) { 313 if (DebugFile.trace) DebugFile.decIdent(); 314 throw new IllegalStateException ("SessionHandler properties not set"); 315 } 316 if (null==sOutAccountName) { 317 if (DebugFile.trace) DebugFile.decIdent(); 318 throw new NullPointerException ("SessionHandler account name not set"); 319 } 320 if (DebugFile.trace) DebugFile.writeln("new SilentAuthenticator("+sOutAccountName+", ...)"); 321 SilentAuthenticator oAuth = new SilentAuthenticator(sOutAccountName, sOutAuthStr); 322 if (DebugFile.trace) DebugFile.writeln("Session.getInstance([Properties],[SilentAuthenticator])"); 323 oSmtpSession = Session.getInstance(oProps, oAuth); 324 } 325 if (DebugFile.trace) { 326 DebugFile.decIdent(); 327 DebugFile.writeln("End SessionHandler.getSmtpSession() : " + oSmtpSession); 328 } 329 return oSmtpSession; 330 } 332 334 341 public Store getStore() throws NoSuchProviderException , MessagingException { 342 if (DebugFile.trace) { 343 DebugFile.writeln("Begin SessionHandler.getStore()"); 344 DebugFile.incIdent(); 345 } 346 if (null==oMailStore) { 347 if (null==sInHostName) { 348 if (DebugFile.trace) DebugFile.decIdent(); 349 throw new NullPointerException ("SessionHandler host name not set"); 350 } 351 if (DebugFile.trace) DebugFile.writeln("Session.getStore()"); 352 oMailStore = getSession().getStore(); 353 if (DebugFile.trace) DebugFile.writeln("Store.connect("+sInHostName+","+sInAccountName+", ...)"); 354 getStore().connect(sInHostName, sInAccountName, sInAuthStr); 355 bIsStoreConnected = true; 356 } 357 if (DebugFile.trace) { 358 DebugFile.decIdent(); 359 DebugFile.writeln("End SessionHandler.getStore() : " + oMailStore); 360 } 361 return oMailStore; 362 } 364 366 373 public Transport getTransport() 374 throws NoSuchProviderException ,MessagingException { 375 if (DebugFile.trace) { 376 DebugFile.writeln("Begin SessionHandler.getTransport()"); 377 DebugFile.incIdent(); 378 } 379 if (null==oMailTransport) { 380 if (DebugFile.trace) DebugFile.writeln("Session.getTransport()"); 381 oMailTransport = getSmtpSession().getTransport(); 382 oMailTransport.connect(); 383 } 384 if (DebugFile.trace) { 385 DebugFile.decIdent(); 386 DebugFile.writeln("End SessionHandler.getTransport() : " + oMailTransport); 387 } 388 return oMailTransport; 389 } 391 393 public Folder getFolder(String sFolderName) 394 throws NoSuchProviderException ,MessagingException { 395 getStore(); 396 if (null==oMailStore) 397 return null; 398 else { 399 return oMailStore.getFolder(sFolderName); 400 } 401 } 403 405 public URLName getURL() { 406 if (null==oURLSession) { 407 if (DebugFile.trace) DebugFile.writeln("new URLName(jdbc://, "+sInHostName+", -1, "+sMBoxDir+", "+sInAccountName+", ...)"); 408 oURLSession = new URLName ("jdbc://", sInHostName, -1, sMBoxDir, sInAccountName, sInAuthStr); 409 } 410 return oURLSession; 411 } 412 413 415 public boolean isStoreConnected() { 416 return bIsStoreConnected; 417 } 418 419 421 public boolean isTransportConnected() { 422 return bIsTransportConnected; 423 } 424 425 427 public void sendMessage(Message oMsg) 428 throws NoSuchProviderException ,SendFailedException ,ParseException , 429 MessagingException ,NullPointerException { 430 if (DebugFile.trace) { 431 DebugFile.writeln("Begin SessionHandler.sendMessage([Message])"); 432 DebugFile.incIdent(); 433 } 434 oMsg.setSentDate(new java.util.Date ()); 435 if (DebugFile.trace) DebugFile.writeln("Transport.send(Message)"); 436 Transport.send(oMsg); 437 if (DebugFile.trace) { 438 DebugFile.decIdent(); 439 DebugFile.writeln("End SessionHandler.sendMessage()"); 440 } 441 } 443 445 public void sendMessage(Message oMsg, Address [] aAddrs) 446 throws NoSuchProviderException ,SendFailedException ,ParseException , 447 MessagingException ,NullPointerException { 448 if (DebugFile.trace) { 449 DebugFile.writeln("Begin SessionHandler.sendMessage([Message],Address[])"); 450 DebugFile.incIdent(); 451 } 452 oMsg.setSentDate(new java.util.Date ()); 453 if (DebugFile.trace) DebugFile.writeln("Transport.send(Message,Address[])"); 454 Transport.send(oMsg,aAddrs); 455 if (DebugFile.trace) { 456 DebugFile.decIdent(); 457 DebugFile.writeln("End SessionHandler.sendMessage()"); 458 } 459 } 461 463 public void sendMessage (Message oMsg, 464 Address [] aAdrFrom, Address [] aAdrReply, 465 Address [] aAdrTo, Address [] aAdrCc, Address [] aAdrBcc) 466 throws NoSuchProviderException ,SendFailedException ,ParseException , 467 MessagingException ,NullPointerException { 468 if (DebugFile.trace) { 469 DebugFile.writeln("Begin SessionHandler.sendMessage([Message],Address[],Address[],Address[],Address[],Address[])"); 470 DebugFile.incIdent(); 471 } 472 oMsg.addFrom(aAdrFrom); 473 if (null==aAdrReply) 474 oMsg.setReplyTo(aAdrReply); 475 else 476 oMsg.setReplyTo(aAdrFrom); 477 if (aAdrTo!=null) oMsg.addRecipients(javax.mail.Message.RecipientType.TO, aAdrTo); 478 if (aAdrCc!=null) oMsg.addRecipients(javax.mail.Message.RecipientType.CC, aAdrCc); 479 if (aAdrBcc!=null) oMsg.addRecipients(javax.mail.Message.RecipientType.BCC, aAdrBcc); 480 oMsg.setSentDate(new java.util.Date ()); 481 if (DebugFile.trace) DebugFile.writeln("Transport.send(Message)"); 482 Transport.send(oMsg); 483 if (DebugFile.trace) { 484 DebugFile.decIdent(); 485 DebugFile.writeln("End SessionHandler.sendMessage()"); 486 } 487 } 489 491 public void close() 492 throws MessagingException { 493 if (DebugFile.trace) { 494 DebugFile.writeln("Begin SessionHandler.close()"); 495 DebugFile.incIdent(); 496 } 497 if (null!=oMailStore) { 498 if (isStoreConnected()) { 499 if (DebugFile.trace) DebugFile.writeln("Store.close()"); 500 oMailStore.close(); 501 } 502 oMailStore = null; 503 } 504 if (null!=oMailTransport) { 505 if (isTransportConnected()) { 506 if (DebugFile.trace) DebugFile.writeln("Transport.close()"); 507 oMailTransport.close(); 508 } 509 oMailTransport=null; 510 } 511 oMailSession=null; 512 oSmtpSession=null; 513 if (DebugFile.trace) { 514 DebugFile.decIdent(); 515 DebugFile.writeln("End SessionHandler.close()"); 516 } 517 } 519 } 520 | Popular Tags |