springboot-mail邮件服务
springboot版本:2.1.3.RELEASE
邮箱开启POP3服务以及查看服务密码引导:163邮箱如何开始POP3协议,获取授权码
目录
一、导入依赖 (可在I/O中勾选)
<!--springboot邮件服务-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
二、添加配置文件配置
-
properties文件
#发送者邮箱地址
mail.fromMail.addr=xxx@163.com
#用户名 格式:** @**.com 随时
spring.mail.username=xxx@163.com
#服务密码,不是邮箱登录密码
spring.mail.password=************
spring.mail.default-encoding=UTF-8
#邮箱服务器地址 格式:smtp.**.com
spring.mail.host=smtp.163.com
-
yml文件
spring:
#基本配置
mail:
#用户名 格式:** @**.com 随时
username: xxx@163.com
#服务密码,不是邮箱登录密码
password: ************
#编码格式
default-encoding: utf-8
#邮箱服务器地址 格式:smtp.**.com
host: smtp.163.com
#发送者邮箱地址
mail:
fromMail:
addr: xxx@163.com
三、实现代码
1、MailService 接口
public interface MailService {
void sendSimpleMail(String to, String subject, String content);
void sendHtmlMail(String to, String subject, String content, String filePath);
void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId);
}
2、MailServiceImpl 实现类
@Component
public class MailServiceImpl implements MailService {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private JavaMailSender mailSender;
//引入配置文件中的发送者邮箱地址
@Value("${mail.fromMail.addr}")
private String from;
//简单格式
@Override
public void sendSimpleMail(String to, String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(content);
try {
mailSender.send(message);
logger.info("简单邮件已经发送。");
} catch (Exception e) {
logger.error("发送简单邮件时发生异常!", e);
}
}
//可以任意HTML内容
@Override
public void sendHtmlMail(String to, String subject, String content, String filePath) {
MimeMessage message = mailSender.createMimeMessage();
try {
//true表示需要创建一个multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
//设置上传文件
//添加多个附件可以使用多条 helper.addAttachment(fileName, file)
FileSystemResource file = new FileSystemResource(new File(filePath));
String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
helper.addAttachment(fileName, file);
mailSender.send(message);
logger.info("html邮件发送成功");
} catch (MessagingException e) {
logger.error("发送html邮件时发生异常!", e);
}
}
//静态资源文件 例如:图片
@Override
public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId){
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
//添加多个图片可以使用多条 <img src='cid:" + rscId + "' > 和 helper.addInline(rscId, res) 来实现
FileSystemResource file = new FileSystemResource(new File(rscPath));
helper.addInline(rscId, file);
mailSender.send(message);
logger.info("嵌入静态资源的邮件已经发送。");
} catch (MessagingException e) {
logger.error("发送嵌入静态资源的邮件时发生异常!", e);
}
}
}
3、MailServiceTest 测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class MailServiceTest {
@Autowired
private MailService mailService;
@Test
public void testSimpleMail() throws Exception {
mailService.sendSimpleMail("xxx@qq.com", "test simple mail", " hello this is simple mail");
}
@Test
public void testHtmlMail() throws Exception {
String content = "<html>\n" +
"<body>\n" +
" <h3>hello world ! 这是一封Html邮件!</h3>\n" +
"</body>\n" +
"</html>";
mailService.sendHtmlMail("xxx@qq.com", "test simple mail", content, "F:\\1.jpg");
}
//静态资源
@Test
public void sendInlineResourceMail2() {
String rscId = "neo006";
String content = "<html><body>这是有图片的邮件:<img src=\'cid:" + rscId + "\' ></body></html>";
String imgPath = "F:\\1.jpg";
mailService.sendInlineResourceMail("xxx@qq.com", "主题:这是有图片的邮件", content, imgPath, rscId);
}
}
四、扩展
1、添加多个附件可以使用多条
helper.addAttachment(fileName,file);
2、添加多个图片可以使用多条
<img src='cid:" + rscId + "' > 和 helper.addInline(rscId, res)
【原创系列】原文链接:https://blog.csdn.net/qq_43227967/article/details/88972681