1 概述
Spring Boot
整合邮件服务,包含发送一般的文本邮件以及带附件的邮件。
2 邮箱抉择
这里抉择的是QQ
邮箱作为发送的邮箱,当然也能够抉择其余的邮箱,只是具体的配置不一样。
应用QQ
邮箱的话,须要在集体设置中开启SMTP
服务:
发送短信后实现验证即可,会有一个受权码,先复制下来保留。
3 具体实现
3.1 依赖
提供了starter
:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId></dependency>
gradle
:
implementation 'org.springframework.boot:spring-boot-starter-mail'
3.2 邮件接口
只有两个简略的接口,一个是发送纯文本的,一个是发送带附件的:
public interface MailService { void sendSimpleMail(String to,String subject,String content); void sendAttachmentMail(String to, String subject, String content, Path file) throws MessagingException;}
3.3 接口实现
@Service@RequiredArgsConstructor(onConstructor = @__(@Autowired))public class MailServiceImpl implements MailService{ private final JavaMailSender sender; @Value("${spring.mail.username}") 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); sender.send(message); } @Override public void sendAttachmentMail(String to, String subject, String content, Path file) throws MessagingException { MimeMessage message = sender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message,true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content); helper.addAttachment(file.getFileName().toString(),new FileSystemResource(file)); sender.send(message); }}
JavaMailSender
是Spring Boot
携带的邮件发送接口,注入后能够发送SimpleMailMessage
以及MimeMessage
类型的信息。
SimpleMailMessage
:简略的邮件信息对象,封装了一些常见的属性,比方寄信地址以及收信地址,发送日期,主题,内容等MimeMessage
:发送MIME
类型的邮件信息,MIME
指的是Multipurpose Internet Mail Extensiosns
,是形容音讯内容类型的因特网规范,能蕴含文本,图像,音频,视频以及其余应用程序专用的数据MimeMessageHelper
:用于设置MimeMessage
属性的类,能够利用其中的addAttachment
增加附件setFrom
/setTo
/setSubject
/setText
:别离示意设置寄信地址
/收信地址
/主题
/内容
3.4 测试类
@SpringBootTest@RequiredArgsConstructor(onConstructor = @__(@Autowired))class DemoApplicationTests { private final MailService service; @Test void contextLoads() throws URISyntaxException, MessagingException { service.sendSimpleMail("xxx@xxx.com","这是主题","这是内容"); service.sendAttachmentMail("xxxx@xx.com","这是主题","这是内容", Path.of(Objects.requireNonNull(getClass().getClassLoader().getResource("pic/1.jpg")).toURI())); //附件为resources下pic/1.jpg service.sendAttachmentMail("xxxx@xxx.com","这是主题","这是内容", Path.of("/","srv","http","1.jpg")); //附件为/srv/http/1.jpg }
发送文本间接指定主题和内容即可,发送带附件的话:
- 如果是
resources
下的内容,应用getClass().getClassLoader().getReource("xxx/xxx")
- 如果是绝对路径,应用
Path.of("/","path1","path2",...,"filename")
3.5 配置文件
spring: mail: host: smtp.qq.com username: xxxxxxx@qq.com password: xxxxxxxxxx port: 465 properties: mail: smtp: ssl: enable: true auth: true starttls: enable: true required: true
作为Demo
应用只须要批改username
以及password
即可。
username
:发送的用户邮箱password
:不是邮箱明码,而是受权码,就是方才开启SMTP
服务呈现的受权码
其余配置阐明:
host
:SMTP
服务器地址port
:端口,能够抉择465
/587
,host
以及port
能够参考QQ
邮箱文档properties
:外面都是一些平安设置,开启SSL
以及认证等
3.6 测试
批改测试类的邮箱,运行单元测试即可。
如果没通过,能够参考这里,列举了常见的错误码以及可能的解决方案。
4 加密
因为用户名以及明码都间接写在了配置文件中,如果泄露的话会很危险,因而须要对配置文件进行加密。
具体的话能够参考笔者之前的原力打算文章,戳这里。
4.1 依赖
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>3.0.3</version></dependency>
gradle
:
implementation("com.github.ulisesbocchio:jasypt-spring-boot-starter:3.0.3")
4.2 配置文件
配置文件只须要加上加密口令即可:
jasypt: encryptor: password: test
默认应用的是PBE
加密算法,PBE
其实是一种组合加密算法,默认是采纳HCMA
算法(混合CMA-ES
算法)+SHA512
音讯摘要算法+AES256
对称加密算法。
另外,如果不想在配置文件间接写上加密的口令,能够应用以下三种办法对口令进行参数化:
命令行参数(运行时设置):
java -jar xxx.jar --jasypt.encryptor.password=test
利用环境变量(运行时设置):
java -Djasypt.encryptor.password=test -jar xxx.jar
零碎环境变量(在配置文件中设置):
jasypt: encryptor: password: ${TEST}# 示意获取环境变量TEST的值作为加密口令
4.3 测试类
新建一个测试类:
@SpringBootTest@RequiredArgsConstructor(onConstructor = @__(@Autowired))public class EncryptAndDecrypt { private final StringEncryptor encryptor; @Value("${spring.mail.username}") private String username; @Value("${spring.mail.password}") private String password; @Test public void encrypt() { System.out.println(encryptor.encrypt(username)); System.out.println(encryptor.encrypt(password)); } @Test public void decrypt() { System.out.println(username); System.out.println(password); }}
4.4 获取密文
假如明文如下:
运行encrypt
即可,输入如下:
4.5 替换明文
加上前缀ENC(
以及后缀)
去替换明文:
4.6 测试
获取明文间接运行decrypt
即可,输入:
这样就实现加密了。
5 源码
Java
版:
- Github
- 码云
- CODE.CHINA
Kotlin
版:
- Github
- 码云
- CODE.CHINA