javaMail学习(与spring集成)
2017-05-16 18:00
176 查看
1. 邮件发送的过程
2. 邮件发送的相关API
3. 邮件发送的工具类MailUtil的提取
4. Spring对javaMail的支持
Spring邮件抽象层的主要包为org.springframework.mail。它包括了发送电子邮件的主要接口MailSender,和其对象SimpleMailMessage,它封装了简单邮件的属性如from, to,cc, subject,text。
包里还包含一棵以MailException为根的checked Exception继承树,它们提供了对底层邮件系统异常的高级别抽象。 要获得关于邮件异常层次的更丰富的信息。
为了使用JavaMail中的一些特色,
比如MIME类型的信件, spring提供了MailSender的一个子接口,
即org.springframework.mail.javamail.JavaMailSender。Spring还提供了一个回调接口org.springframework.mail.javamail.MimeMessagePreparator,
用于准备JavaMail的MIME信件。
这里简单的介绍了如何使用spring发送各种形式的邮件以及配置。
1、在src目录下建立mail.properties文件里边包含一下内容
mail.host=smtp.qq.com
mail.username=你的邮箱名
mail.password=你的邮箱密码
mail.from=发送方的邮箱
2、使用spring配置
5. 发送简单邮件
6. 发送带有图片的邮件,以嵌入HTML的方式
7. 发送带附件的邮件
8. 用户注册时的邮件发送
注意,在注册邮箱时,为了能发送成功,请进行如下调整。
2. 邮件发送的相关API
3. 邮件发送的工具类MailUtil的提取
4. Spring对javaMail的支持
Spring邮件抽象层的主要包为org.springframework.mail。它包括了发送电子邮件的主要接口MailSender,和其对象SimpleMailMessage,它封装了简单邮件的属性如from, to,cc, subject,text。
包里还包含一棵以MailException为根的checked Exception继承树,它们提供了对底层邮件系统异常的高级别抽象。 要获得关于邮件异常层次的更丰富的信息。
为了使用JavaMail中的一些特色,
比如MIME类型的信件, spring提供了MailSender的一个子接口,
即org.springframework.mail.javamail.JavaMailSender。Spring还提供了一个回调接口org.springframework.mail.javamail.MimeMessagePreparator,
用于准备JavaMail的MIME信件。
这里简单的介绍了如何使用spring发送各种形式的邮件以及配置。
1、在src目录下建立mail.properties文件里边包含一下内容
mail.host=smtp.qq.com
mail.username=你的邮箱名
mail.password=你的邮箱密码
mail.from=发送方的邮箱
2、使用spring配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 加载Properties文件 --> <bean id="configurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:mail.properties</value> </list> </property> </bean> <bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage"> <property name="from"> <value>${mail.from}</value> </property> <!-- 查看SimpleMailMessage源码还 4000 可以注入标题,内容等 --> </bean> <!-- 申明JavaMailSenderImpl对象 --> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="defaultEncoding" value="UTF-8" /> <property name="host" value="${mail.host}" /> <property name="username" value="${mail.username}" /> <property name="password" value="${mail.password}" /> <property name="javaMailProperties"> <props> <!-- 设置认证开关 --> <prop key="mail.smtp.auth">true</prop> <!-- 启动调试开关 --> <prop key="mail.debug">true</prop> <!-- 设置发送延时 --> <prop key="mail.smtp.timeout">0</prop> </props> </property> </bean> </beans> |
5. 发送简单邮件
import javax.mail.MessagingException; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.mail.MailSender; import org.springframework.mail.SimpleMailMessage; /** * 本类测试邮件发送Html形式 * * @author Eternity_._ * */ public class SingleMailSend { static ApplicationContext actx = new ClassPathXmlApplicationContext( "applicationContext.xml"); static MailSender sender = (MailSender) actx.getBean("mailSender"); static SimpleMailMessage mailMessage = (SimpleMailMessage) actx.getBean("mailMessage"); public static void main(String args[]) throws MessagingException { mailMessage.setSubject("你好"); mailMessage.setText("这个是一个通过Spring框架来发送邮件的小程序"); mailMessage.setTo("9197****1@qq.com"); sender.send(mailMessage); } } |
6. 发送带有图片的邮件,以嵌入HTML的方式
import java.io.File; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; public class SpringAttachedImageMail { public static void main(String[] args) throws MessagingException { ApplicationContext ctx = new ClassPathXmlApplicationContext( "applicationContext.xml"); JavaMailSenderImpl sender = (JavaMailSenderImpl) ctx .getBean("mailSender"); MimeMessage mailMessage = sender.createMimeMessage(); MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true); messageHelper.setFrom("9197****1@qq.com"); messageHelper.setTo("9197****1@qq.com"); messageHelper.setSubject("测试邮件中嵌套图片!!"); // true 表示启动HTML格式的邮件 messageHelper.setText( "<html><head></head><body><h1>hello!!spring image html mail</h1>" +"<a href=http://www.baidu.com>baidu</a>"+ "<img src=cid:image/></body></html>", true); FileSystemResource img = new FileSystemResource(new File("单.png")); messageHelper.addInline("image", img);//跟cid一致 sender.send(mailMessage); System.out.println("邮件发送成功..."); } } |
7. 发送带附件的邮件
import java.io.File; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; public class SpringAttachedImageMail { public static void main(String[] args) throws MessagingException { ApplicationContext ctx = new ClassPathXmlApplicationContext( "applicationContext.xml"); JavaMailSenderImpl sender = (JavaMailSenderImpl) ctx .getBean("mailSender"); MimeMessage mailMessage = sender.createMimeMessage(); MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true); messageHelper.setFrom("9197****1@qq.com"); messageHelper.setTo("9197****1@qq.com"); messageHelper.setSubject("测试邮件中嵌套图片!!"); // true 表示启动HTML格式的邮件 messageHelper.setText( "<html><head></head><body><h1>hello!!spring image html mail</h1>" +"<a href=http://www.baidu.com>baidu</a>"+ "<img src=cid:image/></body></html>", true); FileSystemResource img = new FileSystemResource(new File("单.png")); messageHelper.addAttachment("单.png", file);//添加到附件 sender.send(mailMessage); System.out.println("邮件发送成功..."); } } |
8. 用户注册时的邮件发送
public void saveOrUpdate(User entity) { if(UtilFuns.isEmpty(entity.getId())){ //说明是新增 String id = UUID.randomUUID().toString(); entity.setId(id); entity.getUserInfo().setId(id);//基于一对一的主键关联 //引入Shiro后,进行密码的初始化 entity.setPassword(Encrypt.md5(SysConstant.DEFAULT_PASS, entity.getUserName())); baseDao.saveOrUpdate(entity); //发送邮件 mailMessage.setSubject("你好"); mailMessage.setText("尊敬的"+entity.getUserInfo().getName()+",您的用户名:"+entity.getUserName()+",初始密码为:123456"); mailMessage.setTo(entity.getUserInfo().getEmail()); mailSender.send(mailMessage); }else{ baseDao.saveOrUpdate(entity); } } |
注意,在注册邮箱时,为了能发送成功,请进行如下调整。
相关文章推荐
- Spring视频学习(十五)集成JPA
- Spring学习笔记:spring集成hibernate
- mybatis学习笔记四(与spring3集成)
- 菜鸟学习Spring——60s学会Spring与Hibernate的集成
- 菜鸟学习Spring——60s学会Spring与Hibernate的集成
- Struts2学习:在struts2中集成Spring
- Mybatis学习(5)与spring3集成
- Spring学习(五)——集成MyBatis
- Spring学习(三)——集成 Velocity
- Quartz Spring集成学习笔记
- Spring学习笔记:spring集成hibernate
- 框架学习之Spring 第四节 Spring集成JDBC组件开发
- 框架学习之Spring 第四节 Spring集成JDBC组件开发
- Spring学习(六)——集成memcached客户端
- CXF学习08---CXF与Spring集成并配置拦截器
- Spring视频学习(十六)集成Struts2
- 巴巴运动网学习笔记四之在Web层集成Struts2.3与Spring3.1
- spring学习--集成jdbc
- WebService 学习笔记之 集成Spring和Web容器
- redis 学习笔记(5)-Spring与Jedis的集成