springboot特点:
1.约定大于配置
2.简单快速开发
3.强大的生态链
4.springboot和发送邮件
开发过程:1.基础配置2.文本邮件3.html邮件4.邮件模板5.图片邮件6.附件邮件7.异常处理8.邮件系统
登陆https://start.spring.io/设置项目常用配置项
简单文本邮件
1.引入相关jar包2.配置邮箱参数3.封装SimpleMailMessage4.JavaMailSend进行发送
<!--导入web包和mail包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!--加入thymeleaf包 用于模板邮件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
在application.properties里面配置
spring.mail.password为授权密码
spring.mail.host=smtp.qq.com
spring.mail.username=*******@qq.com
spring.mail.password=********
spring.mail.default-encoding=UTF-8
service
package com.neo.email.hello;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
/**
* @author mu
* @date 2019/6/13
*/
@Service
public class HelloService {
private Logger logger= LoggerFactory.getLogger(this.getClass());
@Value("${spring.mail.username}")
private String from;
@Autowired
private JavaMailSender javaMailSender;
public void sayHello(){
System.out.println("hello World");
}
/**
* 发送文本邮件
* @param to 发给谁
* @param subject 主题
* @param content 内容
*/
public void sendSimpleMail(String to,String subject,String content){
SimpleMailMessage simpleMailMessage=new SimpleMailMessage();
simpleMailMessage.setTo(to);
simpleMailMessage.setSubject(subject);
simpleMailMessage.setText(content);
simpleMailMessage.setFrom(from);
javaMailSender.send(simpleMailMessage);
}
/**
* 发送html邮件
* @param to 发给谁
* @param subject 主题
* @param content 内容
*/
public void sendHtmlMail(String to,String subject,String content) throws MessagingException {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper=new MimeMessageHelper(mimeMessage,true);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content,true);
helper.setFrom(from);
javaMailSender.send(mimeMessage);
}
/**
* 发送附件邮件
* @param to
* @param subject
* @param content
* @param filePath
* @throws MessagingException
*/
public void sendAttachMENTMail(String to,String subject,String content,String filePath) throws MessagingException {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper=new MimeMessageHelper(mimeMessage,true);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content,true);
helper.setFrom(from);
FileSystemResource fileSystemResource = new FileSystemResource(new File(filePath));
String filename = fileSystemResource.getFilename();
helper.addAttachment(filename,fileSystemResource);
//发送两个附件
helper.addAttachment(filename+"_second",fileSystemResource);
javaMailSender.send(mimeMessage);
}
/**
* 发送图片邮件
* @param to
* @param subject
* @param content
* @param filePath
* @param id
* @throws MessagingException
*/
public void sendInLineResouceMail(String to,String subject,String content,String filePath,String id) {
logger.info("发送静态邮件开始,{},{},{},{},{}",to,subject,content,filePath,id);
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper= null;
try {
helper = new MimeMessageHelper(mimeMessage,true);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content,true);
helper.setFrom(from);
FileSystemResource fileSystemResource = new FileSystemResource(new File(filePath));
helper.addInline(id,fileSystemResource);
javaMailSender.send(mimeMessage);
logger.error("发送邮件成功");
} catch (MessagingException e) {
logger.error("发送邮件失败",e);
}
}
}
serviceTest
package com.neo.email.hello;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import javax.annotation.Resource;
import javax.mail.MessagingException;
@RunWith(SpringRunner.class)
@SpringBootTest
public class HelloTest {
@Resource
HelloService helloService;
@Resource
TemplateEngine templateEngine;
@Test
public void helloWorldTest(){
helloService.sayHello();
}
@Test
public void sendSimpleMailTest(){
helloService.sendSimpleMail("mujiachao@binfo-tech.com","文本邮件测试","这是邮件内容");
}
@Test
public void sendHtmlMailTest() throws MessagingException {
String content="<html>\n"+"<body>\n"+"<h3>这是html邮件</h3>\n"+"</body>\n"+"</html>";
helloService.sendHtmlMail("mujiachao@binfo-tech.com","html邮件测试",content);
}
@Test
public void sendAttachmentMailTest() throws MessagingException {
String filePath="C:\\Users\\Administrator\\Desktop\\email.zip";
helloService.sendAttachMENTMail("mujiachao@binfo-tech.com","文本附件邮箱测试","这是附件邮箱内容",filePath);
}
@Test
public void sendInLineResourceMailTest(){
String filePath="C:\\Users\\Administrator\\Desktop\\index.jfif";
String id="mu01";
String content="<html>\n"+"<body>\n"+"这是有图片的邮件<img src=\'cid:"+id+"\'></img>\n"+"<h3>这是图片邮件</h3></body>\n"+"</html>";
helloService.sendInLineResouceMail("mujiachao@binfo-tech.com","文本图片邮箱测试",content,filePath,id);
}
@Test
public void sendTemplateMailTest() throws MessagingException {
Context context=new Context();
context.setVariable("id","000");
String emailTemplate = templateEngine.process("emailTemplate", context);
helloService.sendHtmlMail("mujiachao@binfo-tech.com","模板邮件测试",emailTemplate);
}
}
templates下的emailTemplate.html模板
thymeleaf的一个声明 重点--> xmlns:th="http://thymeleaf.org"
<!DOCTYPE html>
<!--thymeleaf的一个声明 重点-->
<html lang="en" xmlns:th="http://thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>邮件模板</title>
</head>
<body>
你好,感谢你的注册,这是一封验证邮件,请点击下面验证码完成注册,谢谢!
<a href="#" th:href="@{http://www.ityouknow.com/register/{id}(id=${id})}">激活账户</a>
</body>
</html>