SpringBoot中发送邮件发送功能:
1: 引入jar包:
<dependency><!-- 发送邮件 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
进行配置:
spring:
#邮箱配置
mail:
host: smtp.163.com
username: ***********@163.com
password: *******************
default-encoding: UTF-8
# 使用SMTPS协议465端口 SSL证书Socket工厂 防止公网发送失败
properties.mail.smtp.auth=true
properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
properties.mail.smtp.socketFactory.port=465
2: 定义邮件发送接口:
package com.gy.demo.common.config.mail;
import org.springframework.stereotype.Component;
/**
* Description: 电子邮件接口
*
* @author geYang
* @since 2017/12/27
**/
@Component
public interface EmailService{
/**
* TODO 发送简单邮件(TEXT)
* @param to 发送地址
* @param subject 主题
* @param content 内容
*/
void sendSimpleMail(String to, String subject, String content);
/**
* TODO 发送HTML邮件(页面,不能加载图片)
*/
void sendHtmlMail(String to, String subject, String content);
/**
* TODO 发送附件邮件(带文件)
* @param filePath 文件存放地址
* */
void sendFileMail(String to, String subject, String content, String filePath);
/**
* TODO 发送静态资源邮件
* @param resourcePath 路径
* @param resourceId ID
*/
void sendStaticResourceMail(String to, String subject, String content, String resourcePath, String resourceId);
}
3: 实现
package com.gy.demo.common.config.mail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.Component;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
/**
* Description: 邮件发送接口实现
*
* @author geYang
* @since 2017/12/27
**/
@Component
public class EmailServiceImpl implements EmailService {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Value("${spring.mail.username}")
private String FROM;
@Resource
private JavaMailSender javaMailSender;
/* TODO 简单邮件发送 */
@Override
public void sendSimpleMail (String to, String subject, String content) {
logger.info("发送简单邮件");
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(FROM);
message.setTo(to);
message.setSubject(subject);
message.setText(content);
try{
javaMailSender.send(message);
logger.info("简单邮件发送成功");
} catch (Exception e){
logger.error("简单邮件发送异常!",e);
}
}
/* TODO 发送HTML邮件 */
@Override
public void sendHtmlMail (String to, String subject, String content) {
MimeMessage message = javaMailSender.createMimeMessage();
try {
//true表示需要创建一个 multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(FROM);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
javaMailSender.send(message);
logger.info("HTML邮件发送成功");
} catch (MessagingException e) {
logger.error("HTML邮件发送异常!", e);
}
}
/* TODO 发送带附件邮件 */
@Override
public void sendFileMail (String to, String subject, String content, String filePath) {
MimeMessage message = javaMailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(FROM);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
FileSystemResource file = new FileSystemResource(new File(filePath));
String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
helper.addAttachment(fileName, file);
javaMailSender.send(message);
logger.info("带附件邮件发送成功");
} catch (MessagingException e) {
logger.error("带附件邮件发送异常!", e);
}
}
/* TODO 发送静态资源邮件 */
@Override
public void sendStaticResourceMail (String to, String subject, String content, String resourcePath, String resourceId) {
MimeMessage message = javaMailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(FROM);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
FileSystemResource resource = new FileSystemResource(new File(resourcePath));
helper.addInline(resourceId, resource);
javaMailSender.send(message);
logger.info("静态资源邮件发送成功");
} catch (MessagingException e) {
logger.error("静态资源邮件发送异常!", e);
}
}
}
HTML模板,为Thymelesaf:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8"/>
<title>Hello</title>
</head>
<body>
<h1>Hello World Spring Boot</h1>
<h2 th:text="${title}">Thymeleaf</h2>
</body>
</html>
4: 测试:
package com.gy.demo.common.config;
import com.gy.demo.common.config.mail.EmailService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
/**
* Description: 邮件发送测试
*
* @author geYang
* @since 2017/12/27
**/
@SpringBootTest
@RunWith(SpringRunner.class)
public class EmailTest {
@Autowired
private EmailService emailService;
@Autowired
private TemplateEngine templateEngine;
@Test
public void sendSimpleMailTest(){
String to = "572119197@qq.com";
String subject = "主题: 测试";
String content = "内容: 测试测试";
emailService.sendSimpleMail(to,subject,content);
}
@Test
public void sendHtmlMailTest() {
String to = "572119197@qq.com";
String subject = "测试HTML邮件";
String content = "<html><body><h1>Hello World ! 这是一封HTML邮件!</h1></body>\n</html>";
emailService.sendHtmlMail(to,subject,content);
}
@Test
public void sendFileMailTest(){
String to = "572119197@qq.com";
String subject = "测试附件";
String content = "内容: 这是您要的文件";
String filePath = "C:\\Users\\yvdedu.com\\Desktop\\IMGS\\image\\08.jpg";
emailService.sendFileMail(to,subject,content,filePath);
}
@Test
public void sendStaticResourceMailTest(){
String to = "572119197@qq.com";
String subject = "xxx";
String resourceId = "my008";
String content = "<html><body><h1>哈哈..这就是你:</h1><img src=\'cid:" + resourceId + "\' ></body></html>";
String resourcePath = "C:\\Users\\Desktop\\IMGS\\image\\08.jpg";
emailService.sendStaticResourceMail(to,subject,content,resourcePath,resourceId);
}
@Test
public void sendTemplateMail() {
Context context = new Context();
context.setVariable("title", "geYang");
String emailContent = templateEngine.process("index", context);
emailService.sendHtmlMail("572119197@qq.com","主题:这是模板邮件",emailContent);
}
}
参考大神: http://www.cnblogs.com/ityouknow/category/914493.html
项目源码: https://gitee.com/ge.yang/SpringBoot