<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 495,comments - 227,trackbacks - 0

      spring中有集成了javamail,在使用框架的時候用spring來發(fā)送mail也是很方便的。下面是一個spring mail的小例子。

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

      web.xml的代碼:

      <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4"
     xmlns="  xmlns:xsi="  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
       <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="
        xmlns:xsi="     xmlns:aop="     xmlns: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     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"
        " <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="郵件發(fā)送">
     </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發(fā)送郵件,成功的話會跳轉(zhuǎn)到sendok.jsp頁面。


    FeedBack:
    # re: struts2+spring發(fā)送mail
    2012-07-16 14:54 | sdf
    fdfd  回復(fù)  更多評論
      
    # re: struts2+spring發(fā)送mail[未登錄]
    2012-11-26 20:35 | 123
    asdfsadf  回復(fù)  更多評論
      
    主站蜘蛛池模板: 亚洲制服在线观看| 久久精品国产精品亚洲下载| 亚洲综合自拍成人| 一个人看的hd免费视频| 一本色道久久88亚洲综合| 国产偷国产偷亚洲高清在线| 国产精品视频免费一区二区三区| 亚洲欧美国产国产一区二区三区| 亚洲中文无码永久免费 | 在线亚洲人成电影网站色www| 永久免费无码日韩视频| 中文字幕亚洲电影| 中文字幕乱码一区二区免费| 精品亚洲永久免费精品| 久9热免费精品视频在线观看| 久久久久亚洲av无码专区导航| 59pao成国产成视频永久免费| 亚洲日本国产精华液| 在线观看特色大片免费视频| 国产精品亚洲专一区二区三区| 亚洲精品国产高清不卡在线| 中文字幕免费在线看线人动作大片| 亚洲成AV人片在线观看无 | 拨牐拨牐x8免费| 亚洲AV永久无码精品一福利| 亚洲美女精品视频| 久久WWW免费人成一看片| 亚洲一区二区三区在线观看网站| 国产免费直播在线观看视频| 中国一级特黄高清免费的大片中国一级黄色片 | 国产禁女女网站免费看| 无遮挡国产高潮视频免费观看 | 免费无码精品黄AV电影| 国产亚洲日韩在线a不卡| 亚洲国产成人一区二区精品区| 国产成人免费高清激情明星| 免费无毒a网站在线观看| 日本一道在线日本一道高清不卡免费| 免费的黄色网页在线免费观看| 亚洲国产成人久久综合一| 日本高清免费中文在线看|