Spring Boot对Mail功能已经配置了相关的基本配置,我们只需要稍加修改即可使用
官方文档:https://docs.spring.io/spring/docs/5.1.2.RELEASE/spring-framework-reference/integration.html#mail
1、加入 pom 依赖
<!-- Spring Boot 邮件依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- Spring Boot 模板依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--jasypt配置文件加解密-->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
2、对邮箱密码进行加密操作
- 在 yml 配置文件中配置上 加密密码
# 配置文件项加解密密码,此处作为测试,实际情况放在代码中(放在代码中使加密密钥和密文分开)
jasypt.encryptor.password: password
- 生成加密密码
package ch.springboot.demo.email;
import org.jasypt.encryption.StringEncryptor;
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;
@SpringBootTest
@RunWith(SpringRunner.class)
public class CreatePW {
@Autowired
private StringEncryptor stringEncryptor;
/**
* 生成加密密码
*/
@Test
public void testGeneratePassword() {
// 你的邮箱密码
String password = "abcdefg";
// 加密后的密码(注意:配置上去的时候需要加 ENC(加密密码))
String encryptPassword = stringEncryptor.encrypt(password);
String decryptPassword = stringEncryptor.decrypt(encryptPassword);
System.out.println("密码是 : " + password);
System.out.println("加密后密码是 " + encryptPassword);
System.out.println("解密后密码是 " + decryptPassword);
}
}
console->
3、设置邮箱(以 163 为例)
登录163邮箱后 点击设置,选择POP3
POP3/SMTP 取消打钩。然后再点打钩 弹出页面,点击确定
点击开启,需要手机发送短信验证
发送完成后填写授权码,自定义,记住该授权码,需要在代码中配置
到此邮箱配置大功告成
4、application 配置
替换密码为加密后的 授权码,替换用户名为你的邮箱
spring:
mail:
host: smtp.163.com
username: 邮箱@163.com
# 使用 jasypt 加密密码,替换 ENC(加密密码)
# 此处为授权码,并非登录163邮箱的登录密码
password: ENC(P4P5DKYUhWpuHZQUMwCXKOx+Dho3mVMN)
protocol: smtp
default-encoding: UTF-8
properties:
mail.smtp.auth: true
mail.smtp.starttls.enable: true
mail.smtp.starttls.required: true
mail.smtp.ssl.enable: true
mail.display.sendmail: password
# 配置文件项加解密密码,此处作为测试完,实际情况应该注释,而放在代码中(放在代码中使加密密钥和密文分开)
jasypt:
encryptor:
password: password
5、发送邮件
package ch.springboot.demo.email.service;
import javax.mail.MessagingException;
/**
* mail 接口.
*
* @author ch
* @version 1.0.0
* @since 1.0.0
* <p>
* Created at 2019/11/26 10:08
*/
public interface MailService {
/**
* 发送文本邮件
*
* @param to 收件人地址
* @param subject 邮件主题
* @param test 邮件内容
* @param cc 抄送地址
*/
void sendSimpleMail(String to, String subject, String test, String... cc)
throws MessagingException;
/**
* 发送HTML邮件
*
* @param to 收件人地址
* @param subject 邮件主题
* @param content 邮件内容
* @param cc 抄送地址
* @throws MessagingException 邮件发送异常
*/
void sendHtmlMail(String to, String subject, String content, String... cc) throws MessagingException;
/**
* 发送带附件的邮件
*
* @param to 收件人地址
* @param subject 邮件主题
* @param content 邮件内容
* @param filePath 附件地址
* @param cc 抄送地址
* @throws MessagingException 邮件发送异常
*/
void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc) throws MessagingException;
}
package ch.springboot.demo.email.service.impl;
import ch.springboot.demo.email.service.MailService;
import java.io.File;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
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;
/**
* mail 实现类.
*
* @author ch
* @version 1.0.0
* @since 1.0.0
* <p>
* Created at 2019/11/26 10:20 下午
*/
@Service
public class MailServiceImpl implements MailService {
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String from;
@Override
public void sendSimpleMail(String to, String subject, String text, String... cc)
throws MessagingException {
SimpleMailMessage mailMessage = new SimpleMailMessage();
// 邮箱用户名
mailMessage.setFrom(from);
// 邮件接收人
mailMessage.setTo(to);
// 设置邮件主题
mailMessage.setSubject(subject);
// 设置邮件内容
mailMessage.setText(text);
// 设置抄送人
if (cc != null && cc.length != 0) {
mailMessage.setCc(cc);
}
// 发送邮件
mailSender.send(mailMessage);
}
/**
* @param to 收件人地址
* @param subject 邮件主题
* @param content 邮件内容
* @param cc 抄送地址
* @throws MessagingException
*/
@Override
public void sendHtmlMail(String to, String subject, String content, String... cc)
throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
if (cc != null && cc.length != 0) {
helper.setCc(cc);
}
mailSender.send(message);
}
/**
* 发送带附件的邮件
*
* @param to 收件人地址
* @param subject 邮件主题
* @param content 邮件内容
* @param filePath 附件地址
* @param cc 抄送地址
* @throws MessagingException 邮件发送异常
*/
@Override
public void sendAttachmentsMail(String to, String subject, String content, String filePath,
String... cc) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
if (cc != null && cc.length != 0) {
helper.setCc(cc);
}
FileSystemResource file = new FileSystemResource(new File(filePath));
String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
helper.addAttachment(fileName, file);
mailSender.send(message);
}
}
参考文献:https://github.com/xkcoding/spring-boot-demo/tree/master/spring-boot-demo-email