<2008年1月>
          303112345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          留言簿(4)

          隨筆分類(157)

          搜索

          •  

          最新評論

          閱讀排行榜

          直接放代碼上來,經(jīng)過測試成功。使用gmail來作為測試,因為gmail不是使用25端口,并且需要ssl支持,所以需要聲明這些部分。

          serviceContext-moudle_sendmail.xml:

          <beans>
              <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
                <property name="host" >
               <value>smtp.gmail.com</value>
              </property>
            <property name="javaMailProperties">
           <props>
           <prop key="mail.smtp.auth">true</prop>
             <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
              <prop key="mail.smtp.socketFactory.fallback">false</prop>
           <prop key="mail.smtp.timeout">25000</prop>
           </props>
           </property>
           <property name="port">
                  <value>465</value>
                  </property>
           <property name="username">
           <value>your_name@gmail.com</value>
           </property>
           <property name="password">
            <value>your_password</value>
           </property>  
           </bean>
             
              <bean id ="enrollmentMailMessage" class="org.springframework.mail.SimpleMailMessage">
              <property name="to">
               <value>abc@21cn.com</value>
              </property>
              <property name="from">
               <value>your_name@gmail.com</value>
              </property>
              <property name="subject">
               <value>spring mail test</value>
              </property>
              </bean>
             
           <bean id="SendMail" class="com.yourcompany.service.SendMailImpl">
           <property name="mailMessage">
               <ref bean="enrollmentMailMessage"/>
              </property>
           <property name="mailSender">
               <ref bean="mailSender"/>
              </property>   
           </bean>
          </beans>

          SendMailAction.java

          public ActionForward execute(ActionMapping mapping, ActionForm form,
             HttpServletRequest request, HttpServletResponse response) {
            DynaActionForm sendMailForm = (DynaActionForm) form;// TODO Auto-generated method stub
            String strcontent = sendMailForm.getString("content");
            sendMail.sendmail(strcontent);
            return mapping.findForward("ok");
           }
           private SendMail sendMail;
           public void setSendMail(SendMail dao){
            this.sendMail = dao;  
           }

          SendMail.java

          public interface SendMail {
           public void sendmail(String content);
          }

          SendMailImpl.java

          public class SendMailImpl implements SendMail {
          private MailSender mailSender;
          public void setMailSender(MailSender mailSender){
           this.mailSender = mailSender;
          }
           private SimpleMailMessage mailMessage;
           public void setMailMessage(SimpleMailMessage mailMessage){
            this.mailMessage = mailMessage;
           }

           public void sendmail(String content){
            SimpleMailMessage message = new SimpleMailMessage(this.mailMessage);
            StringBuffer messageText = new StringBuffer();
            messageText.append(content);
            message.setText(messageText.toString());
            mailSender.send(message);
           }
          }


          struts-config.xml

              <form-bean name="sendMailForm" type="org.apache.struts.validator.DynaValidatorForm">
                <form-property name="content" type="java.lang.String" />
              </form-bean>

              <action
                attribute="sendMailForm"
                input="/sendmail/SendMail.jsp"
                name="sendMailForm"
                path="/sendMail"
                scope="request"
                validate="true"
                type="com.yourcompany.struts.action.SendMailAction" >
                <forward name="ok" path="/sendmail/SendMailOk.jsp" />
              </action>

          SendMail.jsp

            <html:form action="/sendMail">
             content : <html:text property="content"/><html:errors property="content"/><br/>
             <html:submit/><html:cancel/>
            </html:form>

          validation.xml

             </form>
               <form name="sendMailForm">
               <field property="content" depends="required">
                 <arg key="sendMailForm.content"/>    
                 </field>      
             </form>
          posted on 2008-01-14 18:10 lzj520 閱讀(705) 評論(0)  編輯  收藏 所屬分類: Struts1Spring個人學(xué)習(xí)日記
          主站蜘蛛池模板: 四子王旗| 获嘉县| 渭源县| 霍城县| 广宁县| 曲沃县| 南川市| 邢台县| 安溪县| 涞水县| 虹口区| 含山县| 宁远县| 遵义县| 噶尔县| 永兴县| 晋江市| 兰西县| 山西省| 高碑店市| 景泰县| 乃东县| 武冈市| 肥东县| 晴隆县| 镇平县| 托克托县| 布尔津县| 东源县| 应用必备| 昭觉县| 山阴县| 三亚市| 承德县| 苍梧县| 凯里市| 眉山市| 称多县| 锡林郭勒盟| 庆云县| 永兴县|