1.接口
1 package com.xzl.mail;
2 
3 public interface OrderManager {
4     public void placeOrder(String email);
5 
6 }
2.实现
 1 package com.xzl.mail;
 2 
 3 import javax.mail.Message;
 4 import javax.mail.MessagingException;
 5 import javax.mail.internet.InternetAddress;
 6 import javax.mail.internet.MimeMessage;
 7 
 8 import org.springframework.mail.MailException;
 9 import org.springframework.mail.SimpleMailMessage;
10 import org.springframework.mail.javamail.JavaMailSender;
11 import org.springframework.mail.javamail.MimeMessagePreparator;
12 
13 public class OrderManagerImpl implements OrderManager {
14     private JavaMailSender mailsender;
15      private SimpleMailMessage message;
16     
17     
18          public void setMessage(SimpleMailMessage message)
19          {
20              this.message = message;
21          }
22          public void setMailsender(JavaMailSender mailsender) 
23          {
24              this.mailsender = mailsender;
25      }
26          public void placeOrder(final String email) {
27             
28      
29              MimeMessagePreparator preparator = new MimeMessagePreparator() {
30                 public void prepare(MimeMessage mimeMessage) throws MessagingException {
31                      mimeMessage.setRecipient(Message.RecipientType.TO, 
32                              new InternetAddress(email));
33                      mimeMessage.setFrom(new InternetAddress(message.getFrom()));
34                      /**转换编码为GBK*/
35                      mimeMessage.setSubject(message.getSubject(),"GBK");
36                      mimeMessage.setText(email+"<br>"+message.getSubject()+message.getText(),"GBK");
37                      
38                  }
39              };
40              try{
41                 mailsender.send(preparator);
42              }
43              catch(MailException ex) {
44                  //log it and go on
45                  System.err.println(ex.getMessage());            
46              }
47          }
48 
49 }
50 
3.测试用例
 1 package com.xzl.mail;
 2 
 3 import org.springframework.beans.factory.BeanFactory;
 4 import org.springframework.beans.factory.xml.XmlBeanFactory;
 5 import org.springframework.core.io.ClassPathResource;
 6 import org.springframework.core.io.Resource;
 7 
 8 public class SpringMailTest {
 9 
10     /**
11      * @param args
12      */
13     public static void main(String[] args) {
14         // TODO Auto-generated method stub
15         Resource res = new ClassPathResource("xxx.xml");
16         BeanFactory bf = new XmlBeanFactory(res);
17         OrderManagerImpl mail = (OrderManagerImpl)bf.getBean("orderManager");
18         mail.placeOrder("jo.o@163.com");
19     }
20 
21 }
22 
4.XML文件
 1 <?xml version="1.0" encoding="UTF-8"?>
 2   <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
 3       "http://www.springframework.org/dtd/spring-beans.dtd">
 4   
 5   <beans>
 6       <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
 7           <property name="host">
 8               <value>smtp.163.com</value>
 9           </property>
10          <property name="username">
11              <value>USERNAME</value>
12          </property>
13          <property name="password">
14              <value>PASSWORD</value>
15          </property>
16          <property name="javaMailProperties">
17              <props>
18                  <prop key="mail.smtp.auth">true</prop>
19                  <prop key="mail.smtp.timeout">25000</prop>
20              </props>
21          </property>
22      </bean>
23  
24      <bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
25          <property name="from">
26              <value>EMAIL</value>
27          </property>
28          <property name="subject">
29              <value>标题</value>
30          </property>
31          <property name="text">
32              <value>内容</value>
33          </property>
34      </bean>
35  
36      <bean id="orderManager" class="com.xzl.mail.OrderManagerImpl">
37          <property name="mailsender">
38              <ref bean="mailSender" />
39          </property>
40          <property name="message">
41              <ref bean="mailMessage" />
42          </property>
43      </bean>
44  
45  </beans>
5.完成

ExtJS教程- Hibernate教程-Struts2 教程-Lucene教程