1 6 7 package org.jfox.mail; 8 9 import java.util.Properties ; 10 import javax.mail.Authenticator ; 11 import javax.mail.PasswordAuthentication ; 12 import javax.mail.Session ; 13 import javax.naming.NamingException ; 14 import javax.naming.Reference ; 15 16 import org.jfox.ioc.common.AbstractService; 17 import org.jfox.ioc.ext.ActiveComponent; 18 import org.jfox.jndi.InitialContextHelper; 19 20 23 24 public class MailService extends AbstractService implements ActiveComponent { 25 private String user; 26 private String password; 27 private String m_jndiName = "mail"; 28 29 private final static String mailStoreProtocolKey = "mail.store.protocol"; 30 private String mailStoreProtocol = "pop3"; 31 32 private final static String mailTransportProtocolKey = "mail.transport.protocol"; 33 private String mailTransportProtocol = "smtp"; 34 35 private final static String mailPop3HostKey = "mail.pop3.host"; 36 private String mailPop3Host = "pop3.foxsource.org"; 37 38 private final static String mailSmtpHostKey = "mail.smtp.host"; 39 private String mailSmtpHost = "smtp.foxsource.org"; 40 41 private final static String mailFromKey = "mail.from"; 42 private String mailFrom = "young_yy@hotmail.com"; 43 44 private final static String mailSmtpAuthKey = "mail.smtp.auth"; 45 private boolean mailSmtpAuth = false; 46 47 48 private Session session = null; 49 50 public void setUser(String user) { 51 this.user = user; 52 } 53 54 public String getUser() { 55 return user; 56 } 57 58 public void setPassword(String password) { 59 this.password = password; 60 } 61 62 public String getPassword() { 63 return password; 64 } 65 66 public String getMailStoreProtocol() { 67 return mailStoreProtocol; 68 } 69 70 public void setMailStoreProtocol(String mailStoreProtocol) { 71 this.mailStoreProtocol = mailStoreProtocol; 72 } 73 74 public String getMailTransportProtocol() { 75 return mailTransportProtocol; 76 } 77 78 public void setMailTransportProtocol(String mailTransportProtocol) { 79 this.mailTransportProtocol = mailTransportProtocol; 80 } 81 82 public String getMailPop3Host() { 83 return mailPop3Host; 84 } 85 86 public void setMailPop3Host(String mailPop3Host) { 87 this.mailPop3Host = mailPop3Host; 88 } 89 90 public String getMailSmtpHost() { 91 return mailSmtpHost; 92 } 93 94 public void setMailSmtpHost(String mailSmtpHost) { 95 this.mailSmtpHost = mailSmtpHost; 96 } 97 98 public String getMailFrom() { 99 return mailFrom; 100 } 101 102 public void setMailFrom(String mailFrom) { 103 this.mailFrom = mailFrom; 104 } 105 106 public void setJNDIName(String name) { 107 if(this.isInitialized()) { 108 logger.warn("Mail Service has started, can't change JNDI name."); 109 } 110 else { 111 m_jndiName = name; 112 } 113 } 114 115 public String getJNDIName() { 116 return m_jndiName; 117 } 118 119 public boolean isMailSmtpAuth() { 120 return mailSmtpAuth; 121 } 122 123 public void setMailSmtpAuth(boolean mailSmtpAuth) { 124 this.mailSmtpAuth = mailSmtpAuth; 125 } 126 127 protected void doInit() throws Exception { 128 final PasswordAuthentication pa = new PasswordAuthentication (getUser(), getPassword()); 129 Authenticator auth = new Authenticator () { 130 protected PasswordAuthentication getPasswordAuthentication() { 131 return pa; 132 } 133 }; 134 Properties prop = new Properties (); 135 prop.setProperty(mailStoreProtocolKey, getMailStoreProtocol()); 136 prop.setProperty(mailTransportProtocolKey, getMailTransportProtocol()); 137 prop.setProperty(mailPop3HostKey, getMailPop3Host()); 138 prop.setProperty(mailSmtpHostKey, getMailSmtpHost()); 139 prop.setProperty(mailFromKey, getMailFrom()); 140 141 if(isMailSmtpAuth()) { 142 prop.setProperty(mailSmtpAuthKey, "true"); 143 } 144 145 session = Session.getInstance(prop, auth); 147 148 } 149 150 protected void doDestroy() throws Exception { 151 session = null; 152 } 153 154 public void run() { 155 } 157 158 protected void doStart() throws Exception { 159 bind(session); 160 } 161 162 protected void doStop() { 163 MailSessionObjectFactory.removeMailSession(getJNDIName()); 164 try { 166 unbind(); 167 } 168 catch(NamingException x) { 169 logger.error("unbind failure", x); 170 } 171 } 172 173 private void bind(Session session) throws NamingException { 174 String bindName = getJNDIName(); 175 Reference ref = new Reference (Session .class.getName(), MailSessionObjectFactory.class.getName(), null); 176 InitialContextHelper.getInitialContext().bind(bindName, ref); 177 178 MailSessionObjectFactory.addMailSession(bindName, session); 179 logger.info("Mail Service bound to " + bindName); 180 } 181 182 private void unbind() throws NamingException { 183 String bindName = getJNDIName(); 184 InitialContextHelper.getInitialContext().unbind(bindName); 185 logger.info("Mail service '" + getJNDIName() + "' removed from JNDI"); 186 } 187 188 public static void main(String [] args) { 189 190 } 191 } 192 193 | Popular Tags |