posts - 495,comments - 227,trackbacks - 0

  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="shwwwx@163.com,nabelun@163.com"/>
        <property name="from" value="shwwwx@163.com"/>
        <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页面。

posted on 2009-05-15 16:27 SIMONE 阅读(2194) 评论(2)  编辑  收藏 所属分类: JAVAJSPstruts

FeedBack:
# re: struts2+spring发送mail
2012-07-16 14:54 | sdf
fdfd  回复  更多评论
  
# re: struts2+spring发送mail[未登录]
2012-11-26 20:35 | 123
asdfsadf  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: