- 浏览: 1044904 次
- 性别:
- 来自: 广州
-
文章分类
- 全部博客 (279)
- Apache net (10)
- JBoss Rules (3)
- Seam ACL (1)
- Seam (14)
- Seam JPA高级权限验证 (8)
- 待解决的问题.... (1)
- JAVA (43)
- Dwr (4)
- Ajax4JSF (1)
- JavaScript (27)
- 生活小常识 (17)
- Richfaces (3)
- seam自己经历 (14)
- JDBC (1)
- mysql (6)
- WebService (10)
- Java Web (4)
- Hibernate (13)
- J2EE框架整合 (3)
- Spring (9)
- BEA Weblogic (1)
- XML (1)
- log4j (6)
- CSS (2)
- javaIO文件的读写 (5)
- SVN服务器的安装 (5)
- powerDesigner (2)
- SQL常用语句 (3)
- wicket初学 (5)
- eclipse (7)
- 正则表达式 (1)
- ExtJS (6)
- maven(m2eclipse) (1)
- struts2.0 (9)
- JPA (6)
- struts2.0整合spring2.5 (9)
- linux (6)
- Oracle (5)
- Servlet (3)
- MyEclipseGen (0)
最新评论
-
qq_31247573:
JAVA 获取http返回XML的数据 -
jasmine_20100810:
...
linux下tomcat服务的启动、关闭与错误跟踪 -
weiaiFang0624:
视频下载地址:http://download.csdn.net ...
there is no action mapped for namespace / and action name解决办法 -
p476462534:
JS控制表单form的提交 -
dandongsoft:
aaaaaaaaaaaaaaa
httpClient,JAVA访问http request response
本文主要介绍如何使用简单的Spring邮件抽象层来实现邮件发送功能,对于JavaMail中的API并不做介绍。通过对比JavaMail的 API和Spring的邮件抽象层,我觉得,Spring的邮件抽象层优点就是简化了代码量,并能充分利用IOC功能;缺点就是要使用部分Spring API,使程序与第三方框架耦合。关于这方面的内容,可以参考Spring的参考手册。
闲言少叙,现在就说说Spring邮件抽象层。这里将要用的接口和类有:
1 ) MailSender ,它是发送邮件的主要接口,代码如下:
- public interface MailSender { /*** Send the given simple mail message.
- * @param simpleMessage the message to send
- * @throws org.springframework.mail.MailParseException
- * in case of failure when parsing the message
- * @throws org.springframework.mail.MailAuthenticationException
- * in case of authentication failure
- * @throws org.springframework.mail.MailSendException
- * in case of failure when sending the message
- */
- void send(SimpleMailMessage simpleMessage) throws MailException;
- /**
- * Send the given array of simple mail messages in batch.
- * @param simpleMessages the messages to send
- * @throws org.springframework.mail.MailParseException
- * in case of failure when parsing a message
- * @throws org.springframework.mail.MailAuthenticationException
- * in case of authentication failure
- * @throws org.springframework.mail.MailSendException
- * in case of failure when sending a message
- */
- void send(SimpleMailMessage[] simpleMessages) throws MailException;
- }
public interface MailSender { /*** Send the given simple mail message. * @param simpleMessage the message to send * @throws org.springframework.mail.MailParseException * in case of failure when parsing the message * @throws org.springframework.mail.MailAuthenticationException * in case of authentication failure * @throws org.springframework.mail.MailSendException * in case of failure when sending the message */ void send(SimpleMailMessage simpleMessage) throws MailException; /** * Send the given array of simple mail messages in batch. * @param simpleMessages the messages to send * @throws org.springframework.mail.MailParseException * in case of failure when parsing a message * @throws org.springframework.mail.MailAuthenticationException * in case of authentication failure * @throws org.springframework.mail.MailSendException * in case of failure when sending a message */ void send(SimpleMailMessage[] simpleMessages) throws MailException; }
2 )一个简单邮件信息的实现类 SimpleMailMessage 。
3 ) JavaMailSender 接口,提供使用 JavaMail 中 MimeMessage ,代码如下:
- public interface MailSender { /*** Send the given simple mail message.
- * @param simpleMessage the message to send
- * @throws org.springframework.mail.MailParseException
- * in case of failure when parsing the message
- * @throws org.springframework.mail.MailAuthenticationException
- * in case of authentication failure
- * @throws org.springframework.mail.MailSendException
- * in case of failure when sending the message
- */
- void send(SimpleMailMessage simpleMessage) throws MailException;
- /**
- * Send the given array of simple mail messages in batch.
- * @param simpleMessages the messages to send
- * @throws org.springframework.mail.MailParseException
- * in case of failure when parsing a message
- * @throws org.springframework.mail.MailAuthenticationException
- * in case of authentication failure
- * @throws org.springframework.mail.MailSendException
- * in case of failure when sending a message
- */
- void send(SimpleMailMessage[] simpleMessages) throws MailException;
- }
public interface MailSender { /*** Send the given simple mail message. * @param simpleMessage the message to send * @throws org.springframework.mail.MailParseException * in case of failure when parsing the message * @throws org.springframework.mail.MailAuthenticationException * in case of authentication failure * @throws org.springframework.mail.MailSendException * in case of failure when sending the message */ void send(SimpleMailMessage simpleMessage) throws MailException; /** * Send the given array of simple mail messages in batch. * @param simpleMessages the messages to send * @throws org.springframework.mail.MailParseException * in case of failure when parsing a message * @throws org.springframework.mail.MailAuthenticationException * in case of authentication failure * @throws org.springframework.mail.MailSendException * in case of failure when sending a message */ void send(SimpleMailMessage[] simpleMessages) throws MailException; }
使用 JavaMail ,需要依赖 mail.jar 和 activation.jar 两个包。利用 Spring 的 IOC 优势,可以通过配置文件来配置邮件发送信息。对于 MailSender , Spring 提供了实现类 JavaMailSenderImpl ,可以如下配置 MailSender 信息。
- < bean id = "mailSender" class = "org.springframework.mail.javamail.JavaMailSenderImpl">
- < property name = "host" value = "smtp.126.com">
- < property name = "javaMailProperties" >
- < props >
- < prop key = "mail.smtp.auth" > true
- < prop key = "mail.smtp.timeout" > 20000
- < property name = "username" value = "kafka0102">
- < property name = "password" value = "****">
< bean id = "mailSender" class = "org.springframework.mail.javamail.JavaMailSenderImpl"> < property name = "host" value = "smtp.126.com"> < property name = "javaMailProperties" > < props > < prop key = "mail.smtp.auth" > true < prop key = "mail.smtp.timeout" > 20000 < property name = "username" value = "kafka0102"> < property name = "password" value = "****">
可以想到,如果是在 Service 中调用 mailSender ,可以通过 IOC 将 mailSender 注入到 Service 中。这里给出一个简单的使用演示:
- BeanFactory bf = new ClassPathXmlApplicationContext( "service-applicationContext.xml" );
- MailSender ms = (MailSender)bf.getBean( "mailSender" );
- SimpleMailMessage smm = new SimpleMailMessage();
- smm.setTo( "kafka0102@126.com" );
- smm.setFrom( "kafka0102@126.com" );
- smm.setSubject( "hello" );
- smm.setText( "I am you" );
- ms.send(smm);
BeanFactory bf = new ClassPathXmlApplicationContext( "service-applicationContext.xml" ); MailSender ms = (MailSender)bf.getBean( "mailSender" ); SimpleMailMessage smm = new SimpleMailMessage(); smm.setTo( "kafka0102@126.com" ); smm.setFrom( "kafka0102@126.com" ); smm.setSubject( "hello" ); smm.setText( "I am you" ); ms.send(smm);
其中, SimpleMailMessage 也可以通过配置文件配置使用到的属性。 SimpleMailMessage 不能发送带有附件的邮件,这时就需要 JavaMailSender (它的实现类也是 JavaMailSenderImpl ), MimeMessagePreparator 的实现类和部分 JavaMail API 。 mailSender 的配置不变,添加一个实现 MimeMessagePreparator 的类 OneMimeMessagePreparator ,代码如下。
- import java.util.Date; import javax.activation.DataHandler;
- import javax.activation.FileDataSource;
- import javax.mail.Message;
- import javax.mail.Multipart;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeBodyPart;
- import javax.mail.internet.MimeMessage;
- import javax.mail.internet.MimeMultipart;
- import org.springframework.mail.javamail.MimeMessagePreparator;
- public class OneMimeMessagePreparator implements MimeMessagePreparator{
- public void prepare(MimeMessage mm) throws Exception {
- mm.setRecipient(Message.RecipientType. TO , new InternetAddress( "kafka0102@126.com" ));
- mm.setFrom( new InternetAddress( "kafka0102@126.com" ));
- mm.setSubject( "I am you" );
- Multipart mp = new MimeMultipart();
- MimeBodyPart mbp = new MimeBodyPart();
- mbp.setText( "hello kafka0102" );
- mp.addBodyPart(mbp);
- String[] files = new String[]{
- "/home/kafka0102/Document/ 常用基础算法 .txt" ,
- "/home/kafka0102/Document/21164.html" };
- for (String f : files){
- MimeBodyPart mbpFile = new MimeBodyPart();
- FileDataSource fds = new FileDataSource(f);
- mbpFile.setDataHandler( new DataHandler(fds));
- mbpFile.setFileName(fds.getName());
- mp.addBodyPart(mbpFile);
- }
- mm.setContent(mp);
- mm.setSentDate( new Date());
- }
- }
import java.util.Date; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Message; import javax.mail.Multipart; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import org.springframework.mail.javamail.MimeMessagePreparator; public class OneMimeMessagePreparator implements MimeMessagePreparator{ public void prepare(MimeMessage mm) throws Exception { mm.setRecipient(Message.RecipientType. TO , new InternetAddress( "kafka0102@126.com" )); mm.setFrom( new InternetAddress( "kafka0102@126.com" )); mm.setSubject( "I am you" ); Multipart mp = new MimeMultipart(); MimeBodyPart mbp = new MimeBodyPart(); mbp.setText( "hello kafka0102" ); mp.addBodyPart(mbp); String[] files = new String[]{ "/home/kafka0102/Document/ 常用基础算法 .txt" , "/home/kafka0102/Document/21164.html" }; for (String f : files){ MimeBodyPart mbpFile = new MimeBodyPart(); FileDataSource fds = new FileDataSource(f); mbpFile.setDataHandler( new DataHandler(fds)); mbpFile.setFileName(fds.getName()); mp.addBodyPart(mbpFile); } mm.setContent(mp); mm.setSentDate( new Date()); } }
OneMimeMessagePreparator 的功能就是配置 MimeMessage ,其中添加了两个附件。下面就是简单的使用方法。
- BeanFactory bf = new ClassPathXmlApplicationContext("service-applicationContext.xml");
- JavaMailSender ms = (JavaMailSender)bf.getBean("mailSender");
- ms.send(new OneMimeMessagePreparator());
BeanFactory bf = new ClassPathXmlApplicationContext("service-applicationContext.xml"); JavaMailSender ms = (JavaMailSender)bf.getBean("mailSender"); ms.send(new OneMimeMessagePreparator());
发表评论
-
看看Spring的contextLoader都做了什么
2010-06-21 22:01 10597public class ContextLoader { ... -
寻找spring框架的启动入口 ContextLoaderListener
2010-06-21 21:58 12323spring在web下的入口在配置文件web.xml的监听器中 ... -
User Role userRole
2010-02-23 11:38 1753条件说明:在用户注册时候,选择role,然后再插入到数据库表中 ... -
spring的applicationContext.xml中使用属性文件*.properties
2009-08-21 15:46 1809我们可以生成如下的属性文件(/WEB-INF/jdbc.pro ... -
spring和quartz进行定时邮件发送 (转)
2009-08-21 15:36 1278一 发送邮件的基类: Java代码 p ... -
怎样使用Spring发邮件?(转)
2009-08-21 15:35 1595关于Spring的邮件发送的功能演示: (我把程序中一些敏感 ... -
Spring的MVC framework
2009-08-21 15:23 1366下面列举一下Spring的MV ... -
Spring XML配置的12个技巧
2009-08-21 15:17 1063Spring是一个强有力的java程序框架,其被广泛应用于 ...
相关推荐
利用spring下的一个类进行邮件发送,内附具体用法
主要介绍了Spring Boot中利用JavaMailSender发送邮件的方法示例, 相信使用过Spring的众多开发者都知道Spring提供了非常好用的JavaMailSender接口实现邮件发送。在Spring Boot的Starter模块中也为此提供了自动化配置...
在spring利用javamail,quartz定时发送邮件
这是入门级的利用spring发送邮件的例子,中文乱码解决。值得参考,
NULL 博文链接:https://bastengao.iteye.com/blog/1064392
Spring的任务执行与调度、Spring Web应用的国际化支持、AJAX和Spring结合的访问模式、利用Spring发 送电子邮件、Spring JMS消息应用编程、教学资源全文检索设计、Java应用的报表打印、网络考试系统设 计、Spring应用...
主要介绍了Spring Boot利用Java Mail实现邮件发送,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
主要为大家详细介绍了spring+maven实现发送邮件功能,利用spring提供的邮件工具来发送邮件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
19.3. 发送一条消息 19.3.1. 使用消息转换器 19.3.2. SessionCallback 和ProducerCallback 19.4. 接收消息 19.4.1. 同步接收 19.4.2. 异步接收 - 消息驱动的POJOs 19.4.3. SessionAwareMessageListener 接口 ...
2. Spring 2.0 的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 更简单的XML配置 2.2.2. 新的bean作用域 2.2.3. 可扩展的XML编写 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的...
Spring Framework 开发参考手册 Rod Johnson Juergen Hoeller Alef Arendsen Colin Sampaleanu Rob Harrop Thomas Risberg Darren Davison Dmitriy Kopylenko Mark Pollack Thierry Templier Erwin ...
1.这是一个在根目录下的mail.properties文件中配好发送邮件地址和接收邮箱地址的实例,利用freeMarker模板作为展示,在测试类中提供配固定接收邮箱地址和手动输入接收邮箱地址两种方法进行发送试验,结果都能实现...
2.6.4. 将Spring 应用程序上下文部署为JCA adapter 2.6.5. 计划任务 2.6.6. 对Java 5 (Tiger) 支持 2.7. 移植到Spring 2.5 2.7.1. 改变 2.8. 更新的样例应用 2.9. 改进的文档 I. 核心技术 3. IoC(控制反转)...
利用Spring框架开发的发送邮件,最简单,最好用。下载下来可以直接运行的
2. Spring 2.0和 2.5的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 新的bean作用域 2.2.2. 更简单的XML配置 2.2.3. 可扩展的XML编写 2.2.4. Annotation(注解)驱动配置 2.2.5. 在classpath中自动搜索组件...
spring Boot默认就是使用thymeleaf模板引擎的,下面这篇文章主要给大家介绍了关于在Spring Boot中利用Thymeleaf发送Email的方法教程,文中通过示例代码介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们...
ssh框架搭建,采用最新struts2、spring3、hibernate4搭建环境,导入数据库,直接能运行(界面不是很完善),项目中采用velocity模版实现发送邮件功能、实现利用验证码登陆、文件上传下载功能及报表导出功能
Spring框架为开发提供了一系列的解决方案,比如利用控制反转的核心特性,并通过依赖注入实现控制反转来实现管理对象生命周期容器化,利用面向切面编程进行声明式的事务管理,整合多种持久化技术管理数据访问,提供...
12.2.1配置邮件发送器 12.2.2构建电子邮件 12.3调度任务 12.3.1使用javatimer调度任务 12.3.2使用quartz调度器 12.3.3按调度计划调用方法 12.4使用jmx管理springbean 12.4.1将springbean输出为mbean 12.4.2...
此外,还利用到了Spring的任务调度和邮件支持。当竞价成功之后会发送邮件到用于用以确认。本系统将DWR和Spring无缝整合在一起,既充分利用了Spring容器的强大,也利用了DWR作为Ajax框架的便捷。