001: /*
002: * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/factory/SendMailFactory.java,v 1.2 2001/03/26 03:36:25 glenn Exp $
003: * $Revision: 1.2 $
004: * $Date: 2001/03/26 03:36:25 $
005: *
006: * ====================================================================
007: *
008: * The Apache Software License, Version 1.1
009: *
010: * Copyright (c) 1999 The Apache Software Foundation. All rights
011: * reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions
015: * are met:
016: *
017: * 1. Redistributions of source code must retain the above copyright
018: * notice, this list of conditions and the following disclaimer.
019: *
020: * 2. Redistributions in binary form must reproduce the above copyright
021: * notice, this list of conditions and the following disclaimer in
022: * the documentation and/or other materials provided with the
023: * distribution.
024: *
025: * 3. The end-user documentation included with the redistribution, if
026: * any, must include the following acknowlegement:
027: * "This product includes software developed by the
028: * Apache Software Foundation (http://www.apache.org/)."
029: * Alternately, this acknowlegement may appear in the software itself,
030: * if and wherever such third-party acknowlegements normally appear.
031: *
032: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
033: * Foundation" must not be used to endorse or promote products derived
034: * from this software without prior written permission. For written
035: * permission, please contact apache@apache.org.
036: *
037: * 5. Products derived from this software may not be called "Apache"
038: * nor may "Apache" appear in their names without prior written
039: * permission of the Apache Group.
040: *
041: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
042: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
043: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
044: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
045: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
046: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
047: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
048: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
049: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
050: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
051: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
052: * SUCH DAMAGE.
053: * ====================================================================
054: *
055: * This software consists of voluntary contributions made by many
056: * individuals on behalf of the Apache Software Foundation. For more
057: * information on the Apache Software Foundation, please see
058: * <http://www.apache.org/>.
059: *
060: * [Additional notices, if required by prior licensing conditions]
061: *
062: */
063:
064: package org.apache.naming.factory;
065:
066: import java.security.AccessController;
067: import java.security.PrivilegedAction;
068: import java.util.Hashtable;
069: import java.util.Properties;
070: import java.util.Enumeration;
071: import javax.mail.Session;
072: import javax.mail.internet.InternetAddress;
073: import javax.mail.internet.MimeMessage;
074: import javax.mail.internet.MimePart;
075: import javax.mail.internet.MimePartDataSource;
076: import javax.naming.Name;
077: import javax.naming.Context;
078: import javax.naming.Reference;
079: import javax.naming.RefAddr;
080: import javax.naming.spi.ObjectFactory;
081:
082: /**
083: * Factory class that creates a JNDI named javamail MimePartDataSource
084: * object which can be used for sending email using SMTP.
085: * <p>
086: * Can be configured in the DefaultContext or Context scope
087: * of your server.xml configuration file.
088: * <p>
089: * Example:
090: * <p>
091: * <pre>
092: * <Resource name="mail/send" auth="CONTAINER"
093: * type="javax.mail.internet.MimePartDataSource"/>
094: * <ResourceParams name="mail/send">
095: * <parameter><name>factory</name>
096: * <value>org.apache.naming.factory.SendMailFactory</value>
097: * </parameter>
098: * <parameter><name>mail.smtp.host</name>
099: * <value>your.smtp.host</value>
100: * </parameter>
101: * <parameter><name>mail.smtp.user</name>
102: * <value>someuser</value>
103: * </parameter>
104: * <parameter><name>mail.from</name>
105: * <value>someuser@some.host</value>
106: * </parameter>
107: * <parameter><name>mail.smtp.sendpartial</name>
108: * <value>true</value>
109: * </parameter>
110: * <parameter><name>mail.smtp.dsn.notify</name>
111: * <value>FAILURE</value>
112: * </parameter>
113: * <parameter><name>mail.smtp.dsn.ret</name>
114: * <value>FULL</value>
115: * </parameter>
116: * </ResourceParams>
117: * </pre>
118: *
119: * @author Glenn Nielsen Rich Catlett
120: */
121:
122: public class SendMailFactory implements ObjectFactory {
123: // The class name for the javamail MimeMessageDataSource
124: protected final String DataSourceClassName = "javax.mail.internet.MimePartDataSource";
125:
126: public Object getObjectInstance(Object RefObj, Name Nm,
127: Context Ctx, Hashtable Env) throws Exception {
128: final Reference Ref = (Reference) RefObj;
129:
130: // Creation of the DataSource is wrapped inside a doPrivileged
131: // so that javamail can read its default properties without
132: // throwing Security Exceptions
133: if (Ref.getClassName().equals(DataSourceClassName)) {
134: return AccessController
135: .doPrivileged(new PrivilegedAction() {
136: public Object run() {
137: // set up the smtp session that will send the message
138: Properties props = new Properties();
139: // enumeration of all refaddr
140: Enumeration list = Ref.getAll();
141: // current refaddr to be set
142: RefAddr refaddr;
143: // set transport to smtp
144: props
145: .put("mail.transport.protocol",
146: "smtp");
147:
148: while (list.hasMoreElements()) {
149: refaddr = (RefAddr) list.nextElement();
150:
151: // set property
152: props.put(refaddr.getType(),
153: (String) refaddr.getContent());
154: }
155: MimeMessage message = new MimeMessage(
156: Session.getInstance(props));
157: try {
158: String from = (String) Ref.get(
159: "mail.from").getContent();
160: message.setFrom(new InternetAddress(
161: from));
162: message.setSubject("");
163: } catch (Exception e) {
164: }
165: MimePartDataSource mds = new MimePartDataSource(
166: (MimePart) message);
167: return mds;
168: }
169: });
170: } else { // We can't create an instance of the DataSource
171: return null;
172: }
173: }
174: }
|