Tomcat6中JavaMail JNDI配置

最经在研究Tomcat,今天看到JNDI这一章,在测试Mail Session的配置时,发现了一个问题:在使用需要配置密码时,Tomcat会报错。

错误:
root cause

java.lang.NoClassDefFoundError: javax/mail/Authenticator
org.apache.naming.factory.MailSessionFactory.getObjectInstance(MailSessionFactory.java:105)
。。。。。。。。。。。。

root cause

java.lang.ClassNotFoundException: javax.mail.Authenticator

。。。。。。。。。。。。
 

(我的Tomcat是6.0.20,JDK1.6update14)

 

参看Tomcat的源码:

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.naming.factory;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.naming.Name;
import javax.naming.Context;
import javax.naming.RefAddr;
import javax.naming.Reference;
import javax.naming.spi.ObjectFactory;

/**
 * <p>Factory class that creates a JNDI named JavaMail Session factory,
 * which can be used for managing inbound and outbound electronic mail
 * messages via JavaMail APIs.  All messaging environment properties
 * described in the JavaMail Specification may be passed to the Session
 * factory; however the following properties are the most commonly used:</p>
 * <ul>
 * <li>
 * <li><strong>mail.smtp.host</strong> - Hostname for outbound transport
 *     connections.  Defaults to <code>localhost</code> if not specified.</li>
 * </ul>
 *
 * <p>This factory can be configured in a <code>&lt;DefaultContext&gt;</code>
 * or <code>&lt;Context&gt;</code> element in your <code>conf/server.xml</code>
 * configuration file.  An example of factory configuration is:</p>
 * <pre>
 * &lt;Resource name="mail/smtp" auth="CONTAINER"
 *           type="javax.mail.Session"/&gt;
 * &lt;ResourceParams name="mail/smtp"&gt;
 *   &lt;parameter&gt;
 *     &lt;name&gt;factory&lt;/name&gt;
 *     &lt;value&gt;org.apache.naming.factory.MailSessionFactory&lt;/value&gt;
 *   &lt;/parameter&gt;
 *   &lt;parameter&gt;
 *     &lt;name&gt;mail.smtp.host&lt;/name&gt;
 *     &lt;value&gt;mail.mycompany.com&lt;/value&gt;
 *   &lt;/parameter&gt;
 * &lt;/ResourceParams&gt;
 * </pre>
 *
 * @author Craig R. McClanahan
 * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (Tue, 24 Oct 2006) $
 */

public class MailSessionFactory implements ObjectFactory {


    /**
     * The Java type for which this factory knows how to create objects.
     */
    protected static final String factoryType = "javax.mail.Session";


    /**
     * Create and return an object instance based on the specified
     * characteristics.
     *
     * @param refObj Reference information containing our parameters, or null
     *  if there are no parameters
     * @param name The name of this object, relative to context, or null
     *  if there is no name
     * @param context The context to which name is relative, or null if name
     *  is relative to the default initial context
     * @param env Environment variables, or null if there are none
     *
     * @exception Exception if an error occurs during object creation
     */
    public Object getObjectInstance(Object refObj, Name name, Context context,
				    Hashtable env) throws Exception 
    {

        // Return null if we cannot create an object of the requested type
	final Reference ref = (Reference) refObj;
        if (!ref.getClassName().equals(factoryType))
            return (null);

        // Create a new Session inside a doPrivileged block, so that JavaMail
        // can read its default properties without throwing Security
        // exceptions.
        //
        // Bugzilla 31288, 33077: add support for authentication.
        return AccessController.doPrivileged( new PrivilegedAction() {
		public Object run() {

                    // Create the JavaMail properties we will use
                    Properties props = new Properties();
                    props.put("mail.transport.protocol", "smtp");
                    props.put("mail.smtp.host", "localhost");

                    String password = null;

                    Enumeration attrs = ref.getAll();
                    while (attrs.hasMoreElements()) {
                        RefAddr attr = (RefAddr) attrs.nextElement();
                        if ("factory".equals(attr.getType())) {
                            continue;
                        }

                        if ("password".equals(attr.getType())) {
                            password = (String) attr.getContent();
                            continue;
                        }

                        props.put(attr.getType(), (String) attr.getContent());
                    }

                    Authenticator auth = null;
                    if (password != null) {
                        String user = props.getProperty("mail.smtp.user");
                        if(user == null) {
                            user = props.getProperty("mail.user");
                        }
                        
                        if(user != null) {
                            final PasswordAuthentication pa = new PasswordAuthentication(user, password);
                            auth = new Authenticator() {
                                    protected PasswordAuthentication getPasswordAuthentication() {
                                        return pa;
                                    }
                                };
                        }
                    }

                    // Create and return the new Session object
                    Session session = Session.getInstance(props, auth);
                    return (session);

		}
	    } );

    }


}

 

第105行的代码是:

return AccessController.doPrivileged( new PrivilegedAction() {

 

如果不是AccessController的问题,那么我就不晓得问题出在哪了。自己写了个MySessionFactory的类:

package tomcat.jndi.provider;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.naming.Context;
import javax.naming.Name;
import javax.naming.RefAddr;
import javax.naming.Reference;
import javax.naming.spi.ObjectFactory;

public class MySessionFactory implements ObjectFactory {

	public Object getObjectInstance(Object refObj, Name name, Context context,
			Hashtable<?, ?> environment) throws Exception {
		final Reference ref = (Reference) refObj;

		// 创建默认的Properties属性集
		Properties props = new Properties();
		props.put("mail.transport.protocol", "smtp");
		props.put("mail.smtp.host", "localhost");

		String password = null;

		// 遍历所有的设定参数
		Enumeration<RefAddr> attrs = ref.getAll();

		while (attrs.hasMoreElements()) {
			RefAddr attr = (RefAddr) attrs.nextElement();

			// 忽略factory参数
			if ("factory".equals(attr.getType())) {
				continue;
			}

			// 密码的设定使用“password”
			if ("password".equals(attr.getType())) {
				password = (String) attr.getContent();
				continue;
			}

			// 其他的参数,如调试、验证等,直接设置在properties中
			props.put(attr.getType(), (String) attr.getContent());
		}

		Authenticator auth = null;

		if (password != null) {

			// 在properties中寻找用户的邮件信息
			String user = props.getProperty("mail.smtp.user");
			if (user == null) {
				user = props.getProperty("mail.user");
			}

			if (user != null) {
				final PasswordAuthentication pa = new PasswordAuthentication(
						user, password);
				auth = new Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return pa;
					}
				};
			}
		}

		// Create and return the new Session object
		Session session = Session.getInstance(props, auth);
		return (session);

	}

}

 修改META-INF/context.xml文件:

	<Resource name="mail/Session" auth="Container"
		type="javax.mail.Session"
		factory="tomcat.jndi.provider.MySessionFactory"
		mail.smtp.auth="true"
		mail.smtp.host="smtp.sohu.com"
		mail.smtp.user="XXX@sohu.com"
		mail.debug="true"
		password="XXXXXX"/>
 

MySessionFactory基本上是仿照Tomcat的MailSessionFactory写的, 测试竟然正常了!!!不知道是不是 AccessController的问题。也不清楚是否是我的配置在某些细微之处错了。先记录一下,研究过AccessController之后,再修改本文!!也欢迎大家和我一起探讨这个问题!!

 

 

问题解决了:

从报错的具体细节,推测出是jar包的导入出问题了。删除 application/WEB-INF/lib目录下的mail.jar,在Tomcat/lib目录中添加mail.jar这样就可以使用 MailSessionFactory了!!

 

再细心地核对了一下Tomcat的说明文档:

写道
Download the JavaMail API. The JavaMail API requires the Java Activation Framework (JAF) API as well. The Java Activation Framework can be downloaded from Sun's site.

This download includes 2 vital libraries for the configuration; activation.jar and mail.jar. Unpackage both distributions and place them into $CATALINA_HOME/lib so that they are available to Tomcat during the initialization of the mail Session Resource. Note: placing these jars in both $CATALINA_HOME/lib and a web application's lib folder will cause an error, so ensure you have them in the $CATALINA_HOME/lib location only.

 

呵呵,是自己看的时候漏掉了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值