tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gl...@apache.org
Subject cvs commit: jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/factory SendMailFactory.java
Date Sun, 11 Mar 2001 15:04:10 GMT
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
   * <http://www.apache.org/>.
   *
   * [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.
   * <p>
   * Requires the following environment properties:
   * <li>smtphost - SMTP server host name
   * <li>user - SMTP user
   * <li>from - default From: email address
   * <p>
   * Can be configured in the DefaultContext or Context scope
   * of your server.xml configuration file.
   * <p>
   * Example:
   * <p>
   * <pre>
   * &lt;Resource name="mail/send" auth="CONTAINER"
   *           type="javax.mail.internet.MimePartDataSource"/>
   * &lt;ResourceParams name="mail/send">
   *   &lt;parameter>&lt;name>factory&lt;/name>
   *     &lt;value>org.apache.naming.factory.SendMailFactory&lt;/value>
   *   &lt;/parameter>
   *   &lt;parameter>&lt;name>smtphost&lt;/name>
   *     &lt;value>your.smtp.host.net&lt;/value>
   *   &lt;/parameter>
   *   &lt;parameter>&lt;name>user&lt;/name>
   *     &lt;value>userid&lt;/value>
   *   &lt;/parameter>
   *   &lt;parameter>&lt;name>from&lt;/name>
   *     &lt;value>joeuser@some.mail.domain.net&lt;/value>
   *   &lt;/parameter>
   * &lt;/ResourceParams>
   * </pre>
   *
   * @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


Mime
View raw message