天天看點

struts2+spring發送mail

spring中有內建了javamail,在使用架構的時候用spring來發送mail也是很友善的。下面是一個spring mail的小例子。

  在這個例子中,除了需要struts和spring的架構外,還需要mail.jar,activation.jar.

  web.xml的代碼:

  <?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4"

 xmlns="http://java.sun.com/xml/ns/j2ee"

 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

 <context-param>

  <param-name>contextConfigLocation</param-name>

  <param-value>classpath*:spring/*.xml</param-value>

 </context-param>

 <listener>

  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

 </listener>

    <!-- Spring refresh Introspector to prevent from out of memory  -->

 <listener>

  <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>

 </listener>

 <!-- Support session scope, Spring bean -->

 <listener>                                                       

      <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>

 </listener>

 <!-- Struts2 Action Mapping-->

 <filter>

        <filter-name>struts2</filter-name>

        <filter-class>

            org.apache.struts2.dispatcher.FilterDispatcher

        </filter-class>

    </filter>

    <listener>

        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

    <filter-mapping>

        <filter-name>struts2</filter-name>

        <url-pattern>/*</url-pattern>

    </filter-mapping>

   <!-- session time out -->

 <session-config>

  <session-timeout>30</session-timeout>

 </session-config>

 <!-- default index -->

 <welcome-file-list>

  <welcome-file>send.jsp</welcome-file>

 </welcome-file-list>

</web-app>

Java類MailAction.java:

package action;

import org.springframework.mail.SimpleMailMessage;

import org.springframework.mail.javamail.JavaMailSenderImpl;

import com.opensymphony.xwork2.Action;

import com.opensymphony.xwork2.ActionSupport;

public class MailAction extends ActionSupport{

 private JavaMailSenderImpl mailSender;

 private SimpleMailMessage mailMessage;

  public String sendMail(){

  SimpleMailMessage msg = new SimpleMailMessage(mailMessage);

        msg.setText("Spring Mail Simple!");

        mailSender.send(msg);

        return Action.SUCCESS;

 }

  public void setMailMessage(SimpleMailMessage mailMessage) {

  this.mailMessage = mailMessage;

 }

 public void setMailSender(JavaMailSenderImpl mailSender) {

  this.mailSender = mailSender;

 }

}

spring配置檔案applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:aop="http://www.springframework.org/schema/aop"

    xmlns:tx="http://www.springframework.org/schema/tx"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd

           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"

    default-autowire="byName" default-lazy-init="true">

 <bean id="mailAction" class="action.MailAction">

  <property name="mailSender" ref="mailSender"/>

  <property name="mailMessage" ref="mailMessage"/>

 </bean>

 <bean id="mailSender"

          class="org.springframework.mail.javamail.JavaMailSenderImpl">

        <property name="host" value="smtp.163.com"/>

            <!-- mail  account   -->

        <property name="username" value="shwwwx"/>

        <property name="password" value="wwx1226"/>

        <property name="javaMailProperties">

            <props>

                <prop key="mail.smtp.auth">true</prop>

            </props>

        </property>

    </bean>

      <!-- mail template -->

    <bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">

        <property name="to" value="[email protected],[email protected]"/>

        <property name="from" value="[email protected]"/>

        <property name="subject" value="Mail Simple"/>

    </bean>

</beans>

struts2的配置檔案struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

    <constant name="struts.objectFactory" value="spring" />

    <constant name="struts.devMode" value="false" />

    <constant name="struts.i18n.encoding" value="utf-8" />

    <package name="springtimer"   extends="struts-default">

       <action name="mailsend" class="mailAction" method="sendMail">

        <result>sendok.jsp</result>

        <interceptor-ref name="defaultStack"/>

       </action>

    </package>

</struts>

頁面send.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<html>

  <head>

    <title>My JSP 'send.jsp' starting page</title>

  </head>

  <body>

 <form action="mailsend.action" method="post">

  <input type="submit" value="郵件發送">

 </form>

  </body>

</html>

頁面sendok.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <title>My JSP 'sendok.jsp' starting page</title>

  </head>

  <body>

    Send Mail Success! <br>

  </body>

</html>

運作send.jsp發送郵件,成功的話會跳轉到sendok.jsp頁面。spring中有內建了javamail,在使用架構的時候用spring來發送mail也是很友善的。下面是一個spring mail的小例子。