【原创】Spring邮件抽象层+javamail发送Email

在最近的一个网站项目中,需求要求注册验证使用邮件(当然这个很常见哈),我们开发使用的是spring+hibernate+struts+jstl,所有我想到的使用spring的邮件抽象层结合javamail来发送邮件。具体的文档说明可以参考Spring Framework 开发参考手册 的第17章。具体代码如下(实现很简单的发送):

1.邮件发送service的接口MailService.java

 1 package  com.justinmobile.payease.base.service;
 2
 3 import  javax.mail.internet.MimeMessage;
 4
 5 import  com.justinmobile.payease.base.PayeaseException;
 6
 7 /** */ /**
 8  *TODO
 9  * @author :zpu
10  *2006-7-11 13:40:31
11   */

12
13 public   interface  MailService  {
14     
15      /** */ /**
16      * 给单个邮箱发送简单注册验证邮件
17      *  @param  to
18      *  @param  content
19      *  @throws  PayeaseException
20       */

21      public   void  send(String to,String content)  throws  PayeaseException;
22
23      /** */ /**
24      * 使用MimeMessage发送邮件
25      *  @param  message
26      *  @throws  PayeaseException
27       */

28      public   void  send(MimeMessage message)  throws  PayeaseException;
29 }

30

2.邮件发送service的接口的实现类:MailServiceImp.java
 1 package  com.justinmobile.payease.base.service;
 2
 3 import  javax.mail.internet.MimeMessage;
 4
 5 import  org.springframework.mail.MailException;
 6 import  org.springframework.mail.SimpleMailMessage;
 7 import  org.springframework.mail.javamail.JavaMailSender;
 8
 9 import  com.justinmobile.payease.base.PayeaseException;
10
11 /** */ /**
12 *TODO
13 *@author:zpu
14 *2006-7-11 13:47:01
15 */

16
17 /** */ /**
18 * @author Home.zhang
19 *
20 */

21 public   class  MailServiceImp  implements  MailService  {
22    
23    /** *//** Spring的JavaMail实现 */
24    private JavaMailSender mailSender;
25    /** *//** 简单邮件内容对象 */
26    private SimpleMailMessage mailMessage;
27    
28    public void setMailMessage(SimpleMailMessage mailMessage) {
29        this.mailMessage = mailMessage;
30    }

31
32    public void setMailSender(JavaMailSender mailSender) {
33        this.mailSender = mailSender;
34    }

35
36    public MailServiceImp() {
37        super();
38    }

39
40    public void send(String to, String content) throws PayeaseException {
41        try{
42            mailMessage.setTo(to);
43            mailMessage.setText(content);
44            mailSender.send(mailMessage);
45        }

46        catch(MailException ex) {
47            throw new PayeaseException("base.mailservice.send");            
48        }

49
50    }

51
52    public void send(MimeMessage message) throws PayeaseException {
53        // TODO Auto-generated method stub
54
55    }

56
57}

58

3.spring 的applicationContext.xml中的配置:
 1      <!--  邮件发送  -->
 2      < bean  id ="mailSender"  class ="org.springframework.mail.javamail.JavaMailSenderImpl" >
 3          < property  name ="host"  value ="mail.server.com"   />
 4          < property  name ="username"  value ="zpuser"   />
 5          < property  name ="password"  value ="123456"   />
 6          < property  name ="javaMailProperties" >
 7              < props >
 8                  < prop  key ="mail.smtp.auth" > true </ prop >
 9              </ props >
10          </ property >
11
12      </ bean >
13
14      < bean  id ="mailMessage"  class ="org.springframework.mail.SimpleMailMessage" >
15          < property  name ="from"  value ="[email protected]"   />
16          < property  name ="subject"  value ="PAYEASE注册验证"   />
17      </ bean >
18
19      < bean  id ="mailService"  class ="com.justinmobile.payease.base.service.MailServiceImp" >
20          < property  name ="mailSender"  ref ="mailSender"   />
21          < property  name ="mailMessage"  ref ="mailMessage"   />
22      </ bean >

4.Action中调用:
  省略!

你可能感兴趣的:(【原创】Spring邮件抽象层+javamail发送Email)