在最近的一個網站項目中,需求要求注冊驗證使用郵件(當然這個很常見哈),我們開發使用的是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

?3

?4

?5

?6

?7


?8

?9

10

11

12

13



14

15


16

17

18

19

20

21

22

23


24

25

26

27

28

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

?2

?3

?4

?5

?6

?7

?8

?9

10

11


12

13

14

15

16

17


18

19

20

21



22

23


24

25


26

27

28



29

30

31

32



33

34

35

36



37

38

39

40



41



42

43

44

45

46



47

48

49

50

51

52



53

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="payease@justinmobile.com"?/>
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>

?2

?3

?4

?5

?6

?7

?8

?9

10

11

12

13

14

15

16

17

18

19

20

21

22

4.Action中調用:
? 省略!