Return-Path: Delivered-To: apmail-jakarta-tomcat-dev-archive@jakarta.apache.org Received: (qmail 45393 invoked by uid 500); 11 Mar 2001 15:04:12 -0000 Mailing-List: contact tomcat-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: list-post: Reply-To: tomcat-dev@jakarta.apache.org Delivered-To: mailing list tomcat-dev@jakarta.apache.org Received: (qmail 45384 invoked by uid 500); 11 Mar 2001 15:04:11 -0000 Delivered-To: apmail-jakarta-tomcat-4.0-cvs@apache.org Date: 11 Mar 2001 15:04:10 -0000 Message-ID: <20010311150410.45380.qmail@apache.org> From: glenn@apache.org To: jakarta-tomcat-4.0-cvs@apache.org Subject: cvs commit: jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/factory SendMailFactory.java glenn 01/03/11 07:04:10 Added: catalina/src/share/org/apache/naming/factory SendMailFactory.java Log: New Factory to create a JNDI named MimePartDataSource for sending email using SMTP Revision Changes Path 1.1 jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/factory/SendMailFactory.java Index: SendMailFactory.java =================================================================== /* * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/factory/SendMailFactory.java,v 1.1 2001/03/11 15:04:10 glenn Exp $ * $Revision: 1.1 $ * $Date: 2001/03/11 15:04:10 $ * * ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 1999 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * . * * [Additional notices, if required by prior licensing conditions] * */ package org.apache.naming.factory; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.Hashtable; import java.util.Properties; import javax.mail.Session; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimePart; import javax.mail.internet.MimePartDataSource; import javax.naming.Name; import javax.naming.Context; import javax.naming.Reference; import javax.naming.spi.ObjectFactory; /** * Factory class that creates a JNDI named javamail MimePartDataSource * object which can be used for sending email using SMTP. *

* Requires the following environment properties: *

  • smtphost - SMTP server host name *
  • user - SMTP user *
  • from - default From: email address *

    * Can be configured in the DefaultContext or Context scope * of your server.xml configuration file. *

    * Example: *

    *

       * <Resource name="mail/send" auth="CONTAINER"
       *           type="javax.mail.internet.MimePartDataSource"/>
       * <ResourceParams name="mail/send">
       *   <parameter><name>factory</name>
       *     <value>org.apache.naming.factory.SendMailFactory</value>
       *   </parameter>
       *   <parameter><name>smtphost</name>
       *     <value>your.smtp.host.net</value>
       *   </parameter>
       *   <parameter><name>user</name>
       *     <value>userid</value>
       *   </parameter>
       *   <parameter><name>from</name>
       *     <value>joeuser@some.mail.domain.net</value>
       *   </parameter>
       * </ResourceParams>
       * 
    * * @author Glenn Nielsen */ public class SendMailFactory implements ObjectFactory { // The class name for the javamail MimeMessageDataSource protected final String DataSourceClassName = "javax.mail.internet.MimePartDataSource"; public Object getObjectInstance(Object RefObj, Name Nm, Context Ctx, Hashtable Env) throws Exception { final Reference Ref = (Reference)RefObj; // Creation of the DataSource is wrapped inside a doPrivileged // so that javamail can read its default properties without // throwing Security Exceptions if (Ref.getClassName().equals(DataSourceClassName)) { return AccessController.doPrivileged( new PrivilegedAction() { public Object run() { // set up the smtp session that will send the message Properties props = new Properties(); // set transport to smtp props.put("mail.transport.protocol", "smtp"); // set smtp host props.put("mail.smtp.host", (String)Ref.get("smtphost").getContent()); // set mail user that talks to smtp host props.put("mail.smtp.user", (String)Ref.get("user").getContent()); // set mail from address String from = (String)Ref.get("from").getContent(); props.put("mail.from", from ); MimeMessage message = new MimeMessage( Session.getInstance(props)); try { message.setFrom(new InternetAddress(from)); message.setSubject(""); } catch (Exception e) {} MimePartDataSource mds = new MimePartDataSource( (MimePart)message); return mds; } } ); } else { // We can't create an instance of the DataSource return null; } } } --------------------------------------------------------------------- To unsubscribe, e-mail: tomcat-dev-unsubscribe@jakarta.apache.org For additional commands, email: tomcat-dev-help@jakarta.apache.org