直接放代碼上來,經(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 閱讀(706) 評論(0)  編輯  收藏 所屬分類: Struts1Spring個人學習日記
          主站蜘蛛池模板: 元谋县| 永仁县| 丽水市| 彝良县| 韩城市| 井陉县| 许昌县| 元朗区| 南靖县| 马鞍山市| 获嘉县| 绥宁县| 会同县| 麻城市| 鹰潭市| 新沂市| 阿拉善盟| 鄂伦春自治旗| 荆州市| 青阳县| 永济市| 祥云县| 龙口市| 陕西省| 乐平市| 娄烦县| 罗定市| 股票| 微山县| 玉龙| 泗阳县| 安多县| 顺平县| 望江县| 呼图壁县| 黑龙江省| 宿松县| 安庆市| 桂东县| 甘洛县| 平果县|