1 17 18 package org.apache.naming.factory; 19 20 import java.security.AccessController ; 21 import java.security.PrivilegedAction ; 22 import java.util.Hashtable ; 23 import java.util.Properties ; 24 import java.util.Enumeration ; 25 import javax.mail.Session ; 26 import javax.mail.internet.InternetAddress ; 27 import javax.mail.internet.MimeMessage ; 28 import javax.mail.internet.MimePart ; 29 import javax.mail.internet.MimePartDataSource ; 30 import javax.naming.Name ; 31 import javax.naming.Context ; 32 import javax.naming.Reference ; 33 import javax.naming.RefAddr ; 34 import javax.naming.spi.ObjectFactory ; 35 36 75 76 public class SendMailFactory implements ObjectFactory 77 { 78 protected final String DataSourceClassName = 80 "javax.mail.internet.MimePartDataSource"; 81 82 public Object getObjectInstance(Object RefObj, Name Nm, Context Ctx, 83 Hashtable Env) throws Exception 84 { 85 final Reference Ref = (Reference )RefObj; 86 87 if (Ref.getClassName().equals(DataSourceClassName)) { 91 return AccessController.doPrivileged( new PrivilegedAction () 92 { 93 public Object run() { 94 Properties props = new Properties (); 96 Enumeration list = Ref.getAll(); 98 RefAddr refaddr; 100 props.put("mail.transport.protocol", "smtp"); 102 103 while (list.hasMoreElements()) { 104 refaddr = (RefAddr )list.nextElement(); 105 106 props.put(refaddr.getType(), (String )refaddr.getContent()); 108 } 109 MimeMessage message = new MimeMessage ( 110 Session.getInstance(props)); 111 try { 112 String from = (String )Ref.get("mail.from").getContent(); 113 message.setFrom(new InternetAddress (from)); 114 message.setSubject(""); 115 } catch (Exception e) {} 116 MimePartDataSource mds = new MimePartDataSource ( 117 (MimePart )message); 118 return mds; 119 } 120 } ); 121 } 122 else { return null; 124 } 125 } 126 } 127 | Popular Tags |