首页 > 代码库 > 【Mail】Tomcat提供JNDI方式支持JavaMail(三)

【Mail】Tomcat提供JNDI方式支持JavaMail(三)

流程介绍  

  Tomcat提供了JavaMail的支持,是通过JNDI的方式实现的,具体流程是:

  Tomcat启动的时候,自身产生一个Session对象,放在JNDI容器中给其他项目调用,其他项目只要通过JNDI API就能获取JNDI中的对象,并在项目中使用。

  技术分享

使用步骤

  1. 配置JNDI资源:新建一个javaweb的maven项目,并在webapp/META-INF中新建一个文件context.xml。
    技术分享

    context.xml文件内容如下:
    1 <Context>
    2   <Resource name="mail/Session" auth="Container"
    3             type="javax.mail.Session"
    4             mail.smtp.host="smtp.163.com" mail.smtp.auth="true"
    5             mail.transport.protocol="smtp"
    6             />
    7 </Context>

     

  2. 添加javamail到tomcat的lib目录:Tomcat要新建session对象,那么tomcat就需要引入mail.jar包
    技术分享
  3. 在java程序中通过JNDI API获取mail的session对象:写一个类MailServlet.java,去获取session对象。
    • MailServlet.java
       1 package com.hd.javamail.servlet;
       2 
       3 import java.io.IOException;
       4 
       5 import javax.mail.Address;
       6 import javax.mail.Message;
       7 import javax.mail.MessagingException;
       8 import javax.mail.Session;
       9 import javax.mail.Transport;
      10 import javax.mail.internet.InternetAddress;
      11 import javax.mail.internet.MimeMessage;
      12 import javax.naming.Context;
      13 import javax.naming.InitialContext;
      14 import javax.naming.NamingException;
      15 import javax.servlet.ServletException;
      16 import javax.servlet.http.HttpServlet;
      17 import javax.servlet.http.HttpServletRequest;
      18 import javax.servlet.http.HttpServletResponse;
      19 
      20 
      21 /**
      22  * Servlet implementation class MailServlet
      23  */
      24 public class MailServlet extends HttpServlet {
      25     private static final long serialVersionUID = 1L;
      26 
      27     /**
      28      * Default constructor. 
      29      */
      30     public MailServlet() {
      31         // TODO Auto-generated constructor stub
      32     }
      33 
      34     /**
      35      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
      36      */
      37     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      38         doPost(request, response);
      39     }
      40 
      41     /**
      42      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
      43      */
      44     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      45 
      46         try {
      47             //从tomcat中获取JNDI容器
      48             Context initCtx = new InitialContext();
      49             Context envCtx = (Context) initCtx.lookup("java:comp/env");
      50             //JNDI中获取Mail的Session对象
      51             Session session = (Session) envCtx.lookup("mail/Session");
      52             
      53             //新建一个邮件
      54             Message message = new MimeMessage(session);
      55             //设置发件人
      56             message.setFrom(new InternetAddress("xxxx@163.com"));
      57             //设置多个收件人
      58             message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("123456@qq.com"));
      59             //设置主题
      60             message.setSubject("ha");
      61             //设置文本
      62             message.setText("test");
      63             
      64             //邮件传送对象
      65             Transport transport = session.getTransport();
      66             //设置,邮件服务器地址、用户名、密码
      67             transport.connect("smtp.163.com", "xxxx@163.com", "xxxxxx");
      68             //发送邮件(邮件,地址)
      69             transport.sendMessage(message,  new Address[] {new InternetAddress("123456@qq.com")} );
      70             //关闭连接
      71             transport.close();
      72             
      73             response.getWriter().print("ok!");
      74             
      75         } catch (MessagingException e) {
      76             // TODO Auto-generated catch block
      77             e.printStackTrace();
      78         } catch (NamingException e) {
      79             // TODO Auto-generated catch block
      80             e.printStackTrace();
      81         }
      82     }
      83 
      84 }

       

    • web.xml中配置MailServlet
       1 <!DOCTYPE web-app PUBLIC
       2  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
       3  "http://java.sun.com/dtd/web-app_2_3.dtd" >
       4 
       5 <web-app>
       6     <display-name>test-javamail</display-name>
       7     <servlet>
       8         <servlet-name>MailServlet</servlet-name>
       9         <display-name>MailServlet</display-name>
      10         <description></description>
      11         <servlet-class>com.hd.javamail.servlet.MailServlet</servlet-class>
      12     </servlet>
      13     <servlet-mapping>
      14         <servlet-name>MailServlet</servlet-name>
      15         <url-pattern>/MailServlet</url-pattern>
      16     </servlet-mapping>
      17     
      18 </web-app>

       

    • 注意pom文件中mail.jar的引用,只需在编译的时候使用,运行的时候使用前面tomcat/lib目录下的mail.jar包。所以需要如下配置,否则会出错。
       1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
       3     <modelVersion>4.0.0</modelVersion>
       4     <groupId>com.hd</groupId>
       5     <artifactId>test-javamail</artifactId>
       6     <packaging>war</packaging>
       7     <version>0.0.1-SNAPSHOT</version>
       8     <name>test-javamail Maven Webapp</name>
       9     <url>http://maven.apache.org</url>
      10     <dependencies>
      11     
      12         <!-- javamail -->
      13         <dependency>
      14             <groupId>javax.mail</groupId>
      15             <artifactId>mail</artifactId>
      16             <version>1.4.7</version>
      17             <scope>provided</scope><!-- 仅编译的时候使用,运行是由tomcat提供 -->
      18         </dependency>
      19         
      20         <dependency>
      21             <groupId>javax.servlet</groupId>
      22             <artifactId>javax.servlet-api</artifactId>
      23             <version>3.0.1</version>
      24             <scope>provided</scope>
      25         </dependency>
      26         <dependency>
      27             <groupId>javax.servlet.jsp</groupId>
      28             <artifactId>jsp-api</artifactId>
      29             <version>2.2</version>
      30             <scope>provided</scope>
      31         </dependency>
      32 
      33         <dependency>
      34             <groupId>junit</groupId>
      35             <artifactId>junit</artifactId>
      36             <version>3.8.1</version>
      37             <scope>test</scope>
      38         </dependency>
      39     </dependencies>
      40     <build>
      41         <finalName>test-javamail</finalName>
      42     </build>
      43 </project>

       

测试运行

  在浏览器中访问MailServlet,地址:http://ip:端口/test-javamail/MailServlet
  技术分享

 

【Mail】Tomcat提供JNDI方式支持JavaMail(三)