这次来看看Spring提高的一些有用过的J2EE中使用到的功能,如 发送Email, 执行定时任务...


先来看看发送Email吧, 在Spring中发送Email是很简单的,使用Spring提高的MailSender和MailMessage就可以了,配置代码如下: 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host">
        <value>smtp.163.com</value>
    </property>
<property name="javaMailProperties">
<props>
<!-- 如果要使用用户名和密码验证,这一步需要 -->

       <prop key="mail.smtp.auth">true</prop>
  </props>
 </property>
<property name="username">
    <value>yong hu ming </value>
</property>
<property name="password">
     <value>mi ma</value>
</property>
</bean>
<!-- 简单的message -->

<bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
<property name="to">
<value>shou jian ren</value>
</property>
<property name="from">
<value>ni de you xiang di zhi</value>
</property>
<property name="subject"> <!-- Email 标题 -->
<value>A Spring Mail sender test to my baoyaer</value>
</property>
</bean>
<!-- 测试发送的类 -->

<bean id="testMailSender" class="test.SpringMail.TestSenderMail">
<property name="mailMessage">
<ref bean="mailMessage"/>
</property>
<property name="mailSender">
<ref bean="mailSender"/>
</property>
</bean>


</beans>

 
上面的配置好以后就可以直接发送了, 看看TestSenderMail.java的代码:


package test.mail;

import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

public class TestSenderMail {
  
private MailSender mailSender;
  
private SimpleMailMessage mailMessage;
  
  
public TestSenderMail() {
    
  }

  
  
public SimpleMailMessage getMailMessage() {
    
return mailMessage;
  }

  
public void setMailMessage(SimpleMailMessage mailMessage) {
    
this.mailMessage = mailMessage;
  }

  
public MailSender getMailSender() {
    
return mailSender;
  }

  
public void setMailSender(MailSender mailSender) {
    
this.mailSender = mailSender;
  }

  
  
public void sendMail() {
    SimpleMailMessage message 
= new SimpleMailMessage(mailMessage);
    
//设置email内容, 
    message.setText("测试Spring 发送Email.");
    
    
try {
      mailSender.send(message);
    }
 catch (MailException e) {
      
// TODO Auto-generated catch block
      System.out.println("O . 发送Email失败了.");
      e.printStackTrace();
    }

  }

}



很简单吧. 下面是测试类: TestApp.java


 1package test.mail;
 2
 3import org.springframework.context.ApplicationContext;
 4import org.springframework.context.support.ClassPathXmlApplicationContext;
 5
 6public class TestApp {
 7
 8  /**
 9   * @param args
10   */

11  public static void main(String[] args) {
12    // TODO Auto-generated method stub
13    ApplicationContext context = new ClassPathXmlApplicationContext(
14        "test/mail/mail.xml");
15
16    TestSenderMail sender = (TestSenderMail) context
17        .getBean("testMailSender");
18    sender.sendMail();
19  }

20  //抛出如下异常,是瑞星监控的问题,关闭就可以了
21  /**
22   * DEBUG SMTP: QUIT failed with 250 O . 发送Email失败了.
23   * org.springframework.mail.MailSendException: Could not send mails: 354
24   * 
25   * com.sun.mail.smtp.SMTPSendFailedException: 354
26   * 
27   * at
28   * com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388)
29   * at com.sun.mail.smtp.SMTPTransport.finishData(SMTPTransport.java:1215) at
30   * com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:586)
31   */

32}



呵呵, 现在就可以发送Email了.