1.引入pom
<!-- Javamail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
2,config.properties
mail.host=smtp.163.com
mail.username=你的邮箱名——也就是@前面的东西
mail.password=密码
mail.from=你的邮箱地址
3,applicationContext.xml
加入:
<!-- 简单消息对象创建 -->
<bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
<property name="from" value="${mail.from}"></property>
</bean>
<!-- 2.创建发送器 -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.host}"></property>
<property name="username" value="${mail.username}"></property>
<property name="password" value="${mail.password}"></property>
<property name="defaultEncoding" value="UTF-8"></property>
<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>
JavaMailSenderImpl支持MimeMessages和
SimpleMailMessages。
MimeMessages为复杂邮件模板,支持文本、附件、html、图片等。
SimpleMailMessages实现了MimeMessageHelper,为普通邮件模板,支持文本。
4,applicationContext-service.xml中注值
<bean id="userService" class="cn.itcast.jk.service.impl.UserServiceImpl">
<property name="baseDao" ref="baseDao"></property>
<property name="mailMessage" ref="mailMessage"></property>
<property name="mailSender" ref="mailSender"></property>
</bean>
mailMessage与mailSender注入到UserServiceImpl中
5.UserServiceImpl类中的saveorUpdate方法中加入
//spring整合javaMail需要注入:
private SimpleMailMessage mailMessage;
private JavaMailSender mailSender;
public void setMailMessage(SimpleMailMessage mailMessage) {
this.mailMessage = mailMessage;
}
public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
public void saveOrUpdate(final User entity) {
if(UtilFuns.isEmpty(entity.getId())){
//判断id是否有值
//说明id没有值,说明保存
entity.setState(1); //1代表可用
String id = UUID.randomUUID().toString();
entity.setId(id);
entity.getUserinfo().setId(id);
//设置初始密码 需要将默认的密码加密后保存到数据库
entity.setPassword(Encrypt.md5(SysConstant.DEFAULT_PASS, entity.getUserName()));
//final就是延长对象的生命周期,不然entity只能在saveOrUpdate中使用,使用完成后方法弹栈,而run方法内就无法再使用之前定义好的entity。
//使用spring与javaMail实现新员工入职时邮件的发送
//使用线程并try-catch的目的就是如果邮件发送失败,也不影响信息保存到数据库。邮件发送成为了一个独立的过程。
Thread th = new Thread(new Runnable(){
public void run(){
try {
mailMessage.setTo(entity.getUserinfo().getEmail());
mailMessage.setSubject("新员工入职信息");
mailMessage.setText("欢迎"+entity.getUserinfo().getName()+"加入廊坊思创志远科技有限公司,您在公司的账号:"+entity.getUserName()+",密码:"+SysConstant.DEFAULT_PASS);
mailSender.send(mailMessage);
} catch (MailException e) {
e.printStackTrace();
}
}
});
th.start();
}
baseDao.saveOrUpdate(entity);
}
如此,我们便完成了一个简单邮件的编写,对于复杂邮件,编写及发送如下
/使用JavaMail的MimeMessage,支付更加复杂的邮件格式和内容
MimeMessage msg = mailSender.createMimeMessage();
//创建MimeMessageHelper对象,处理MimeMessage的辅助类
MimeMessageHelper helper = new MimeMessageHelper(msg, true);
//使用辅助类MimeMessage设定参数
helper.setFrom(mailSender.getUsername());
helper.setTo("mosaic@126.com");
helper.setSubject("Hello Attachment");
helper.setText("This is a mail with attachment");
//加载文件资源,作为附件
ClassPathResource file = new ClassPathResource("Chrysanthemum.jpg");
//加入附件
helper.addAttachment("attachment.jpg", file);
// 发送邮件
mailSender.send(smm);
其中MimeMessageHelper为的辅助类MimeMessages。以上包含了以资源文件为附件进行发送。对于普通文件发送方式如下:
FileSystemResource file = new FileSystemResource("C:\\Users\\image1.jpg");
helper.addInline("file", file);
发送html文件
只需要在MimeMessageHelpersetText时将是否是html设为true即可。setText介绍如下:
- setText(String text, booleanhtml)
- Set the given text directly as content in non-multipartmode or as default body part in multipart mode.
示例代码(包括附件)如下:
//第二个参数true,表示text的内容为html
//注意<img/>标签,src='cid:file','cid'是contentId的缩写,'file'是一个标记,需要在后面的代码中调用MimeMessageHelper的addInline方法替代成文件
helper.setText("<body><p>Hello Html Email</p><img src='cid:file'/></body>", true);
FileSystemResource file = new FileSystemResource("C:\\Users\\image1.jpg");
helper.addInline("file", file);