Spring Boot
整合邮件服务,包括发送普通的文本邮件以及带附件的邮件。java
这里选择的是QQ
邮箱做为发送的邮箱,固然也能够选择其余的邮箱,只是具体的配置不同。ios
使用QQ
邮箱的话,须要在我的设置中开启SMTP
服务:git
发送短信后完成验证便可,会有一个受权码,先复制下来保存。github
提供了starter
:算法
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
gradle
:spring
implementation 'org.springframework.boot:spring-boot-starter-mail'
只有两个简单的接口,一个是发送纯文本的,一个是发送带附件的:安全
public interface MailService { void sendSimpleMail(String to,String subject,String content); void sendAttachmentMail(String to, String subject, String content, Path file) throws MessagingException; }
@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
类型的信息。bash
SimpleMailMessage
:简单的邮件信息对象,封装了一些常见的属性,好比寄信地址以及收信地址,发送日期,主题,内容等MimeMessage
:发送MIME
类型的邮件信息,MIME
指的是Multipurpose Internet Mail Extensiosns
,是描述消息内容类型的因特网标准,能包含文本,图像,音频,视频以及其余应用程序专用的数据MimeMessageHelper
:用于设置MimeMessage
属性的类,能够利用其中的addAttachment
添加附件setFrom
/setTo
/setSubject
/setText
:分别表示设置寄信地址
/收信地址
/主题
/内容
@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")
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
便可。ide
username
:发送的用户邮箱password
:不是邮箱密码,而是受权码,就是刚才开启SMTP
服务出现的受权码其余配置说明:
host
:SMTP
服务器地址port
:端口,能够选择465
/587
,host
以及port
能够参考QQ
邮箱文档properties
:里面都是一些安全设置,开启SSL
以及认证等修改测试类的邮箱,运行单元测试便可。
若是没经过,能够参考这里,罗列了常见的错误码以及可能的解决方案。
因为用户名以及密码都直接写在了配置文件中,若是泄露的话会很危险,所以须要对配置文件进行加密。
具体的话能够参考笔者以前的原力计划文章,戳这里。
<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")
配置文件只须要加上加密口令便可:
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的值做为加密口令
新建一个测试类:
@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); } }
假设明文以下:
运行encrypt
便可,输出以下:
加上前缀ENC(
以及后缀)
去替换明文:
获取明文直接运行decrypt
便可,输出:
这样就完成加密了。
Java
版:
Kotlin
版: