从Spring Web应用程序发送电子邮件

时间:2014-02-13 12:21:58

标签: spring

我有请假申请页面:

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<html> 
<head>  
<title> Application </title>       
  <link rel="stylesheet" href="CSS/Master.css">
   </head>
   <body>                   <!-- ----- <center> --->
    <h2> Application Form  </h2>            <!-- -- --</center> ------>
<table>
 <form:form method="POST" action="leaveapplyform.do" commandName="leaveapplyform" modelAttribute="leaveapplyform">    
 <tr>  <td ><spring:message code="label.LeaveType"/> </td>          
    <td>    <form:select path="LeaveType" name="LeaveType" id="choice" onchange="ShowReg(this.selectedIndex)">    
<form:option value="Five Live Carry Forward"></form:option>
<form:option value="Live Carry Forward"></form:option>
<form:option value="Hello"></form:option></form:select> </td>   </tr><br>    
 <tr>  <td > <spring:message code="label.leavePeriod"/></td>

    <td>    <form:select path="leavePeriod" name="leavePeriod" id="choice" onchange="ShowReg(this.selectedIndex)">
            <form:option value="fullDayPackage" >Full Day </form:option>
        <form:option value="halfDayPackage">Half Day</form:option>
    </form:select>
    <br />       
<div id="fullDay" style="display:none"><span class="style12"><spring:message code="label.selectHours"/></span>
<form:select path="selectHours" name="selectHours" id="selecte" onchange="ShowSkill(this.selectedIndex)">
<form:option selected="selected" value="event_one">1 HR</form:option>
<form:option value="event_two">2 HR</form:option>
<form:option value="event_three">3 HR </form:option>
<form:option value="event_four">4 HR</form:option>
<form:option value="event_five">5 HR </form:option>
    </form:select>
</div>     
<div id="halfDay" style="display:none">
</div>    
    </tr><br>    
<tr>   <td>  <spring:message code="label.reason"/>    <br />                        </td>           <td>     <textarea rows="5" cols="22"  name="reason"> </textarea>   </td>   
    </tr>     
    <tr>    <td><spring:message code="label.FirstApprover"/></td>   
        <td>    <form:select path="FirstApprover" name="FirstApprover" >
<form:option value="zafar " >Zak </form:option>
<form:option value="priyanka">Ranka</form:option>
            </form:select> </td>   </tr>      
    <tr>    <td><spring:message code="label.finalApprover"/></td>   
    <td>    <form:select path="finalApprover" name="finalApprover" >
<form:option value="zafarm" >Zak </form:option>
<form:option value="priyanka">Ranka</form:option>
    </form:select> </td>   </tr>         
    <tr>    <td colspan="2"> &nbsp;   <br> </td>   </tr>      
    <tr>    <td > &nbsp;   </td>   
    <td >    <input type="reset"> &nbsp; &nbsp; &nbsp;    <input type="submit" value="Submit" >  </td>   </tr>   
    <tr>    <td colspan="2"> &nbsp;   <br> </td>   </tr>  </form:form>  </table>   
<script type="text/javascript">
    function ShowReg(op) {
        document.getElementById('fullDay').style.display = 'none';
        document.getElementById('halfDay').style.display = 'none';

        if (op == 1) {
            document.getElementById('fullDay').style.display = "block";
        }
        if (op == 2) {
            document.getElementById('halfDay').style.display = "block";
        }

    }
    function ShowSkill(op) {
        document.getElementById('golf').style.display = 'none';
        document.getElementById('other').style.display = 'none';

        if (op == 1) {
            document.getElementById('golf').style.display = "block";
        }
        if (op == 2) {
            document.getElementById('other').style.display = "block";
        }

    }
</script>
<div id="other" style="display:none">
</div>
 </body>
    </html>

另外,在提交时,一些像休假类型原因的内容应该作为电子邮件内容包含在内,而这样做的类是:

@Service("mailService") public class ApplicationMailer { @Autowired private MailSender mailSender;  @Autowired
    private SimpleMailMessage preConfiguredMessage;
   public void sendMail(String to, String subject, String body)
    {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(to);
        message.setSubject(subject);
        message.setText(body);
        mailSender.send(message);
    }
   public void sendPreConfiguredMail(String message)
    {

        public void apply(leaveapply leave) {
    SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage);
            msg.setTo(order.getCustomer().getEmailAddress());
            msg.setText(
                "Dear " + order.getEmpName().getFirstName()
                    + order.getCustomer().getLastName()
                    + ", thank you);
            try{
                this.mailSender.send(msg);
            }
            catch(MailException ex) {
                  System.err.println(ex.getMessage());            
            }}}}

我的spring-sevlet.xml文件是:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/jsp/" />
      <property name="suffix" value=".jsp" />
   </bean>
   <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages"/>     
        <property name="defaultEncoding" value="UTF-8"/>
  </bean>

    <bean id="localeChangeInterceptor"
        class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="lang" />
    </bean>

    <bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="defaultLocale" value="en"/>
    </bean>

        <bean id="handlerMapping"
              class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
            <property name="interceptors">
                <ref bean="localeChangeInterceptor" />
            </property>
     </bean>
</beans>

现在我想为邮件配置它,需要进行哪些更改,有人能告诉我吗?

1 个答案:

答案 0 :(得分:1)

Config Maven

<!-- Java Mail API -->
<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.7</version>
</dependency>

Config Spring(gmail示例)

<!-- Mail configuration. Values are in spring.properties -->
<bean id="mailSender">
  <property name="host" value="smtp.gmail.com" />
  <property name="port" value="587" />
  <property name="username" value="${mail.username}" />
  <property name="password" value="${mail.password}" />

  <property name="javaMailProperties">
    <props>
        <prop key="mail.smtp.auth">true</prop>
        <prop key="mail.smtp.starttls.enable">true</prop>
    </props>
  </property>
</bean>

应该工作!