SSH发送邮件的代码

直接放代码上来,经过测试成功。使用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>[email protected]</value>
 </property>
 <property name="password">
  <value>your_password</value>
 </property>  
 </bean>
   
    <bean id ="enrollmentMailMessage" class="org.springframework.mail.SimpleMailMessage">
    <property name="to">
     <value>[email protected]</value>
    </property>
    <property name="from">
     <value>[email protected]</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>

你可能感兴趣的:(bean,jsp,struts,ssh,Gmail)