使用Spring邮件抽象层发送邮件

使用Spring邮件抽象层发送邮件

Spring提供了一个发送电子邮件的高级抽象层,它向用户屏蔽了底层邮件系统的一些细节,同时负责低层次的代表客户端的资源处理。Spring邮件抽象层的主要包为org.springframework.mail。它包括了发送电子邮件的主要接口MailSender和 封装了简单邮件的属性如from, to,cc, subject, text的值对象叫做SimpleMailMessage。

1、我们定义一个发送邮件的接口:OrderManager.java

1 public interface OrderManager extends BaseManager{

2 /**

3 *email,要发送的邮件地址;

4 *Code:激活码

5 */

6 public void placeOrder(String email);

7 }

2、我们需要对该接口进行实现的方法:OrderManagerImpl.java

1 import javax.mail.Message;

2 import javax.mail.MessagingException;

3 import javax.mail.internet.InternetAddress;

4 import javax.mail.internet.MimeMessage;

5 import org.springframework.mail.MailException;

6 import org.springframework.mail.javamail.JavaMailSender;

7 import org.springframework.mail.javamail.MimeMessagePreparator;

8 import service.OrderManager;

9

11 public class OrderManagerImpl extends BaseManagerImpl implements OrderManager {

12

13 private JavaMailSender mailsender;

14 private MyMailMessage message;

15

16

17 public void setMessage(CityMailMessage message)

18 {

19 this.message = message;

20 }

21 public void setMailsender(JavaMailSender mailsender) {

22 this.mailsender = mailsender;

23 }

24 public void placeOrder(final String email) {

25

26

27 MimeMessagePreparator preparator = new MimeMessagePreparator() {

28 public void prepare(MimeMessage mimeMessage) throws MessagingException {

29 mimeMessage.setRecipient(Message.RecipientType.TO,

30 new InternetAddress(email));

31 mimeMessage.setFrom(new InternetAddress(message.getFrom()));

32 /**转换编码为GBK*/

33 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 }

3、spring配置发送email的applicationContext-email.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 >

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 >

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>

46 </bean>

47

48 <bean >

49 <property name="mailsender">

50 <ref bean="mailSender" />

51 </property>

52 <property name="message">

53 <ref bean="mailMessage" />

54 </property>

55 </bean>

56

57 </beans>

4、最后配置自己的jsp页面以及action

1 package cn.cityyouth.web.action;

2

3 import javax.servlet.http.HttpServletRequest;

4 import javax.servlet.http.HttpServletResponse;

5 import org.apache.struts.action.ActionForm;

6 import org.apache.struts.action.ActionForward;

7 import org.apache.struts.action.ActionMapping;

8 import org.apache.struts.action.ActionMessage;

9 import org.apache.struts.action.ActionMessages;

10 import com.test.service.OrderManager;

11

12 public class SendMailAction extends BaseAction {

13

14 /**

15 * Method execute

16 *

17 * @param mapping

18 * @param form

19 * @param request

20 * @param response

21 * @return ActionForward

22 */

23 public ActionForward execute(ActionMapping mapping, ActionForm form,

24 HttpServletRequest request, HttpServletResponse response) {

25 OrderManager omi=(OrderManager)this.getBean("orderManager");

26 String useremail="123@163.com";

27 omi.placeOrder(useremail);

28 }

29 }

到此所有的开发以结束。

Sring邮件抽象层的主要包是:org.springframework.mail 包。它包含叫MailSender为发送邮件的核心接口和包含简单邮件属性例如from,to,cc,subject,text叫SimpleMailMessage的值对象. 这个包也包含一个检查异常的层次,它支持一个更高级别的抽象超过低级别的邮件系统异常伴随根异常存在MailException. 请参考JavaDocs为更多的信息杂邮件异常层次。

Spring也支持一个MailSender的专用于JavaMail特征例如MIME消息子接口,命名为org.springframework.javamail.JavaMailerSener。它也支持一个为JavaMail MIME信息的准备回调接口,命名为org.springframework.mail.JavaMail.MimeMessagePreparator.