1 23 24 package org.infoglue.cms.util.mail; 25 26 import java.util.Properties ; 27 28 import javax.mail.Session ; 29 30 import org.infoglue.cms.util.CmsPropertyHandler; 31 32 33 38 39 public class MailServiceFactory 40 { 41 42 private static Session session; 44 45 46 49 50 public static synchronized MailService getService() throws Exception 51 { 52 if(session == null) 53 { 54 session = initializeSession(); 55 } 57 58 return new MailService(session); 59 } 60 61 62 65 66 private MailServiceFactory() {} 67 68 69 78 private static Session initializeSession() throws Exception 79 { 80 Properties properties = CmsPropertyHandler.getProperties(); 81 82 Properties props = new Properties (); 83 84 boolean needsAuthentication = false; 85 try 86 { 87 needsAuthentication = new Boolean ((String )properties.get("mail.smtp.auth")).booleanValue(); 88 } 89 catch (Exception ex) 90 { 91 needsAuthentication = false; 92 } 93 94 if (needsAuthentication) 95 { 96 final String uName = (String )(String )properties.get("mail.smtp.user"); 97 final String uPass = (String )(String )properties.get("mail.smtp.password"); 98 99 javax.mail.Authenticator authenticator = new javax.mail.Authenticator () 100 { 101 protected javax.mail.PasswordAuthentication getPasswordAuthentication() 102 { 103 return new javax.mail.PasswordAuthentication (uName, uPass); 104 } 105 }; 106 107 return Session.getInstance(properties, authenticator); 108 } 109 else 110 { 111 return Session.getInstance(properties); 112 } 113 } 114 } | Popular Tags |