赞
踩
- <!--spring boot web的依赖-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
-
- <!-- mail 依赖 -->
- <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>
-
- <!--freemarker模版依赖-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-freemarker</artifactId>
- </dependency>
-
- <!-- Lombok工具 -->
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <version>1.18.4</version>
- <scope>provided</scope>
- </dependency>
- spring:
- # 发送邮件配置
- mail:
- host: smtp.qq.com # 配置 smtp 服务器地址
- port: 587 # smtp 服务器的端口
- username: xxxxxx@qq.com # 配置邮箱用户名(你的邮箱地址)
- password: xxxxxxxxx # 配置申请到的授权码(第6步教程复制的授权码)
- default-encoding: UTF-8 # 配置邮件编码
- properties:
- mail:
- smtp:
- socketFactoryClass: javax.net.ssl.SSLSocketFactory # 配饰 SSL 加密工厂
- debug: true
- from: xxxxxxxx@qq.com # 发送方邮件,配在yml中可方便更改(你的邮箱)
- import java.io.File;
-
- public interface EmailService {
-
- /**
- * 发送文本/简单的邮件
- * @param receiverName 接收人
- * @param title 标题
- * @param content 内容
- */
- void sendStringEmail(String receiverName, String title, String content);
-
- /**
- * 发送大文件/附件的邮件
- * @param receiverName
- * @param title
- * @param content
- * @param file 文件
- */
- void sendBigEmail(String receiverName, String title, String content, File file);
-
- /**
- * 发送模版邮件
- * @param receiverName
- * @param title
- * @param information 模版参数名(html页面)
- */
- void sendTemplateEmail(String receiverName, String title, String information);
- }
- import com.example.email.emaill.EmailConfig;
- import com.example.email.service.EmailService;
- import org.springframework.beans.factory.annotation.Autowired;
- 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 org.thymeleaf.TemplateEngine;
- import org.thymeleaf.context.Context;
-
- import javax.mail.internet.MimeMessage;
- import java.io.File;
- import java.util.HashMap;
- import java.util.Map;
-
- @Service
- public class EmailServiceImpl implements EmailService {
-
- @Autowired
- private EmailConfig emailConfig;
-
- @Autowired
- private JavaMailSender javaMailSender;
-
- @Autowired
- private TemplateEngine templateEngine;
-
- // @Autowired
- // private FreeMarkerConfigurer markerConfigurer;
-
- //发送文本/简单的邮件
- @Override
- public void sendStringEmail(String receiverName, String title, String content) {
- SimpleMailMessage message = new SimpleMailMessage();
- message.setFrom(emailConfig.getEmailFrom());
- message.setTo(receiverName);
- message.setSubject(title);
- message.setText(content);
- javaMailSender.send(message);
- }
-
- //发送大文件/附件的邮件
- @Override
- public void sendBigEmail(String receiverName, String title, String content, File file) {
- MimeMessage message = javaMailSender.createMimeMessage();
- try {
- MimeMessageHelper helper = new MimeMessageHelper(message,true);
- helper.setFrom(emailConfig.getEmailFrom());
- helper.setTo(receiverName);
- helper.setSubject(title);
- helper.setText(content);
- FileSystemResource resource = new FileSystemResource(file);
- helper.addAttachment("附件", resource);
- }catch (Exception e){
- e.printStackTrace();
- }
- javaMailSender.send(message);
- }
-
- //发送模版邮件
- @Override
- public void sendTemplateEmail(String receiverName, String title, String information) {
- MimeMessage message = javaMailSender.createMimeMessage();
- try {
- MimeMessageHelper helper = new MimeMessageHelper(message,true);
- helper.setFrom(emailConfig.getEmailFrom());
- helper.setTo(receiverName);
- helper.setSubject(title);
- //封装模版使用的数据
- Map<String,Object> map = new HashMap<>();
- map.put("username","明世隐");
-
- //1.FreeMarker
- //1-1 获取FreeMarker模版
- //Template markertemplate = markerConfigurer.getConfiguration().getTemplate(information);
- //1-2 将模版内容转为字符串类型并将参数传入
- //String markertTtml = FreeMarkerTemplateUtils.processTemplateIntoString(markertemplate, map);
- //1-3 将字符串作为邮件内容
- //helper.setText(markertTtml,true);
-
- //2.Thymeleaf
- //2-1 获取Thymeleaf模版
- Context context = new Context();
- context.setVariable("username","瑶");
- //2-2 将模版内容转为字符串类型并将参数传入
- String thymeleafHtml = templateEngine.process("thymeleafTemplate", context);
- helper.setText(thymeleafHtml,true);
-
- }catch (Exception e){
- e.printStackTrace();
- }
-
- javaMailSender.send(message);
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。