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
版: