`
xiaolongfeixiang
  • 浏览: 234810 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

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
分享到:
评论

相关推荐

    《程序天下:J2EE整合详解与典型案例》光盘源码

    5.3.2 为Eclipse配置Tomcat插件 5.4 使用Eclipse建立Web开发项目 5.5 Eclipse的常用快捷键 5.5.1 有关格式化的快捷键 5.5.2 有关调试的快捷键 5.5.3 有关重构的快捷键 5.6 小结 第六章 Log4j使用指南 6.1 Log4j介绍 ...

    搞定J2EE:STRUTS+SPRING+HIBERNATE整合详解与典型案例 (2)

    5.3.2 为Eclipse配置Tomcat插件 5.4 使用Eclipse建立Web开发项目 5.5 Eclipse的常用快捷键 5.5.1 有关格式化的快捷键 5.5.2 有关调试的快捷键 5.5.3 有关重构的快捷键 5.6 小结 第六章 Log4j使用指南 6.1 Log4j介绍 ...

    搞定J2EE:STRUTS+SPRING+HIBERNATE整合详解与典型案例 (1)

    5.3.2 为Eclipse配置Tomcat插件 5.4 使用Eclipse建立Web开发项目 5.5 Eclipse的常用快捷键 5.5.1 有关格式化的快捷键 5.5.2 有关调试的快捷键 5.5.3 有关重构的快捷键 5.6 小结 第六章 Log4j使用指南 6.1 Log4j介绍 ...

    搞定J2EE:STRUTS+SPRING+HIBERNATE整合详解与典型案例 (3)

    5.3.2 为Eclipse配置Tomcat插件 5.4 使用Eclipse建立Web开发项目 5.5 Eclipse的常用快捷键 5.5.1 有关格式化的快捷键 5.5.2 有关调试的快捷键 5.5.3 有关重构的快捷键 5.6 小结 第六章 Log4j使用指南 6.1 Log4j介绍 ...

    JSP2.0技术手册pdf(带示例源码).zip

    14-8 JNDI - 数据来源(Data Source) 与连接池(Connection Pool) 14-9 JSTL 的 SQL 标签库 14-10 Connection Pool - Proxool 第十五章 JSP Tag Library 15-1 JSP Tag Library 简介 15-2 一个简单的 Tag Library 范例...

    从Java走向Java+EE+.rar

    3.2 Tomcat的安装和使用 20 3.3 小结 25 第4章 在Java EE中使用XML 26 4.1 什么是XML 26 4.1.1 理解XML 26 4.1.2 XML的语法 27 4.1.3 XML命名空间 31 4.2 XML能用来干什么 32 4.3 用DTD验证XML文档...

    Spring.3.x企业应用开发实战(完整版).part2

    7.10.3 在Tomcat下的配置 7.10.4 在其他Web应用服务器下的配置 7.11 小结 第3篇 数据访问 第8章 Spring对DAO的支持 8.1 Spring的DAO理念 8.2 统一的异常体系 8.2.1 Spring的DAO异常体系 8.2.2 JDBC的异常转换器 ...

    Spring3.x企业应用开发实战(完整版) part1

    7.10.3 在Tomcat下的配置 7.10.4 在其他Web应用服务器下的配置 7.11 小结 第3篇 数据访问 第8章 Spring对DAO的支持 8.1 Spring的DAO理念 8.2 统一的异常体系 8.2.1 Spring的DAO异常体系 8.2.2 JDBC的异常转换器 ...

    Java数据库编程宝典2

    4.5.3 DataSource对象和JNDI 4.5.4 部署和使用DataSource的基本实现 4.6 连接池 4.7 分布式事务处理 4.7.1 分布式事务管理 4.7.2 Connection对象 4.8 SQL语句 4.8.1 Statement对象 4.8.2 PreparedStatement...

    Java数据库编程宝典4

    4.5.3 DataSource对象和JNDI 4.5.4 部署和使用DataSource的基本实现 4.6 连接池 4.7 分布式事务处理 4.7.1 分布式事务管理 4.7.2 Connection对象 4.8 SQL语句 4.8.1 Statement对象 4.8.2 PreparedStatement...

    Java数据库编程宝典1

    4.5.3 DataSource对象和JNDI 4.5.4 部署和使用DataSource的基本实现 4.6 连接池 4.7 分布式事务处理 4.7.1 分布式事务管理 4.7.2 Connection对象 4.8 SQL语句 4.8.1 Statement对象 4.8.2 PreparedStatement...

    Java数据库编程宝典3

    4.5.3 DataSource对象和JNDI 4.5.4 部署和使用DataSource的基本实现 4.6 连接池 4.7 分布式事务处理 4.7.1 分布式事务管理 4.7.2 Connection对象 4.8 SQL语句 4.8.1 Statement对象 4.8.2 PreparedStatement...

    “桂林高新杯”软件设计大赛.doc

    " "J2EE "1、JavaBean、Servlet、JNDI、JavaMail、JSP、JDBC、EJB、Stru" " "ts、Hibernate、JTA、JTS、JMS、Spring、Web Service; " " "2、流程控制、数组、异常处理和面向对象编程; " " "3、集合框架、线程、...

    Apache Geronimo 2.1_ Quick Reference.pdf

    Chapter 6: Security 149 Overview of security standards 149 Java Authentication and Authorization Service (JAAS) 150 Java Authorization Contract for Containers (JACC) 150 The Common Secure ...

Global site tag (gtag.js) - Google Analytics