<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  回復  更多評論
      
    # re: struts2+spring發(fā)送mail[未登錄]
    2012-11-26 20:35 | 123
    asdfsadf  回復  更多評論
      
    主站蜘蛛池模板: 亚洲一区二区三区久久| 亚洲国产综合自在线另类| 黄色毛片免费网站| 午夜色a大片在线观看免费| 亚洲国产视频久久| 女人张开腿等男人桶免费视频| 亚洲一卡2卡3卡4卡国产网站| 在线精品一卡乱码免费| 亚洲成A∨人片在线观看无码| 色影音免费色资源| 国产精品亚洲片在线va| 97无码免费人妻超级碰碰碰碰| 亚洲人成77777在线播放网站不卡| 台湾一级毛片永久免费 | 久久久久亚洲AV无码专区网站| 男人j进女人p免费视频| 亚洲婷婷国产精品电影人久久| 中文在线日本免费永久18近| 亚洲av永久无码精品秋霞电影影院| 国产精品免费一区二区三区四区| 中文字幕亚洲综合精品一区| 国产又黄又爽又猛免费app| 亚洲精品国产av成拍色拍| 亚洲国产精品成人网址天堂| 成人电影在线免费观看| 亚洲国产精品综合久久久| 国产免费私拍一区二区三区| 丝袜捆绑调教视频免费区| 亚洲白色白色在线播放| 国产无遮挡裸体免费视频| 永久免费av无码入口国语片| va天堂va亚洲va影视中文字幕| 免费v片在线观看无遮挡| 国产成年无码久久久免费| 亚洲www77777| 精品亚洲综合久久中文字幕| 99re热免费精品视频观看| 亚洲免费一区二区| 亚洲国产精品综合久久网各| 久久精品亚洲福利| 成年女人男人免费视频播放|