如何从Web应用程序发送电子邮件?

时间:2014-02-13 07:27:23

标签: spring spring-mvc spring-security spring-webflow

我有这个离开申请页面:

<%@ 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的”)     公共类ApplicationMailer     {         @Autowired         私人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());            
            }}}}

任何人都能告诉我这是怎么做的吗?

2 个答案:

答案 0 :(得分:1)

您可以看一下:http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/

您可以声明为服务的课程

在此之后,在您需要此邮件服务的控制器中,您自动连接了您的邮件服务,并且您可以发送邮件。

答案 1 :(得分:1)

看一下spring docs here,它更容易,并提供了许多实用方法。