springboot实现发送文本邮件,html邮件,模板邮件
现在的网站,在注册用户时一般都会对注册用户发送一封邮件,用来验证注册用户的身份,或者用于一些报警提测,比如接口调用量,可用率报警等等
springboot中的 spring-boot-starter-mail
封装了发送邮件的接口,可以实现发送文本邮件,html邮件,发送模板邮件等等功能
pom文件中引入 spring-boot-starter-mail
<dependencies> <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> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
在配置文件中指定发送邮件的相关配置
spring: mail: host: smtp.qq.com username: 你的邮箱 password: 你的秘钥 properties: mail: smtp: auth: true default-encoding: utf-8
EmailService接口提供两个接口:sendSimpleEmail,sendHtmlEmail
/** * @auther: chenmingyu * @date: 2019/2/24 13:00 * @description: 邮件服务 */ public interface EmailService { /** * 发送文本邮件 * @param toMail 接收邮箱 * @param subject 标题 * @param text 文本 * @return */ boolean sendSimpleEmail(String toMail,String subject,String text); /** * 发送html邮件 * @param toMail 接收邮箱 * @param subject 标题 * @param html html内容 * @return */ boolean sendHtmlEmail(String toMail,String subject,String html); }
/** * @author: chenmingyu * @date: 2019/2/24 13:01 * @description: 邮件服务 */ @Service public class EmailServiceImpl implements EmailService { @Resource private JavaMailSender mailSender; /** * 发送邮件邮箱 */ @Value("${spring.mail.username}") private String formMail; @Override public boolean sendSimpleEmail(String toMail,String subject,String text) { try { SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); simpleMailMessage.setFrom(formMail); simpleMailMessage.setTo(toMail); simpleMailMessage.setSubject(subject); simpleMailMessage.setText(text); mailSender.send(simpleMailMessage); System.out.println("发送文本邮件:toMail:"+toMail); return true; }catch (Exception e){ System.out.println("发送文本邮件异常:toMail:"+toMail); e.printStackTrace(); } return false; } @Override public boolean sendHtmlEmail(String toMail, String subject, String html) { try { MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true); mimeMessageHelper.setTo(toMail); mimeMessageHelper.setFrom(formMail); mimeMessageHelper.setText(html,true); mimeMessageHelper.setSubject(subject); mailSender.send(mimeMessage); System.out.println("发送html邮件:toMail:"+toMail); } catch (Exception e) { System.out.println("发送html邮件异常:toMail:"+toMail); e.printStackTrace(); } return false; } }
@RunWith(SpringRunner.class) @SpringBootTest public class SpringbootEmailApplicationTests { @Resource private EmailService emailService; @Test public void sendSimpleMail() { emailService.sendSimpleEmail("1520151594@qq.com","注册提醒","欢迎注册,这是一封测试邮件"); } @Test public void sendHtmlMail() { emailService.sendHtmlEmail("1520151594@qq.com","注册提醒","<h1>欢迎注册,这是一封测试邮件</h1>"); } }
接收到的邮件
在发送一些固定场景中,除了使用HTML发送邮件外更常用的是定义一个模板,使用模板发送邮件,然后替换模板中的一些变化的值
以 thymeleaf
为渲染模板,实现模板的邮件发送
引入 thymeleaf
依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>注册邮件</title> </head> <body> <p><span th:text="${name}"></span>,欢迎注册,这是一封测试邮件</p> </body> </html>
/** * 发送模板邮件 * @param toMail 接收邮箱 * @param subject 标题 * @param templateValue 模板内容填充值 * @return */ boolean sendTemplateEmail(String toMail,String subject,Map<String,Object> templateValue);
@Override public boolean sendTemplateEmail(String toMail, String subject, Map<String,Object> templateValue) { System.out.println("发送html邮件:toMail:"+toMail); try { MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true); mimeMessageHelper.setTo(toMail); mimeMessageHelper.setFrom(formMail); mimeMessageHelper.setSubject(subject); Context context = new Context(); context.setVariables(templateValue); String text = templateEngine.process("template",context); mimeMessageHelper.setText(text, true); mailSender.send(mimeMessage); return true; } catch (Exception e) { System.out.println("发送html邮件异常:toMail:"+toMail); e.printStackTrace(); } return false; }
@Test public void sendTemplateMail() { Map<String,Object> map = new HashMap<>(); map.put("name","叫我明羽"); emailService.sendTemplateEmail("1520151594@qq.com","注册提醒",map); }
模板邮件