SpringBoot整合郵箱發(fā)送郵件
引入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
配置文件
server.port=8082
spring.mail.host=smtp.qq.com
spring.mail.protocol=smtp
spring.mail.default-encoding=UTF-8
spring.mail.password=[POP3/IMAP/SMTP/Exchange/CardDAV 服務(wù) 授權(quán)碼]
spring.mail.username=843566121@qq.com
spring.mail.port=587
spring.mail.properties.mail.stmp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.debug=true
Service層接口及實(shí)現(xiàn)類
/**
* <p>
* 郵件發(fā)送Service層接口
* </p>
*
* @author jpge
* @since 2023-09-23
*/
public interface EmailService {
/**
* 發(fā)送郵箱驗(yàn)證碼
*
* @param mailAddress 郵箱地址
* @param code 驗(yàn)證碼
* @param sec 安全碼
*/
void sendSignUpCaptcha(String mailAddress, String code, Integer sec);
}
import com.edu.vertifycode.mail.service.EmailService;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Date;
/**
* <p>
* 郵件發(fā)送Service層接口 實(shí)現(xiàn)類
* </p>
*
* @author jpge
* @since 2023-09-23
*/
@Service
public class EmailServiceImpl implements EmailService {
@Resource
JavaMailSender javaMailSender;
@Resource
MailProperties mailProperties;
@Resource
TemplateEngine templateEngine;
/**
* 發(fā)送郵箱驗(yàn)證碼
*
* @param mailAddress 郵箱地址
* @param code 驗(yàn)證碼
* @param sec 安全碼
*/
public void sendSignUpCaptcha(String mailAddress, String code, Integer sec) {
MimeMessage msg = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(msg);
try {
//設(shè)置郵件元信息
helper.setTo(mailAddress);
helper.setFrom(mailProperties.getUsername());
helper.setSubject("驗(yàn)證碼");
helper.setSentDate(new Date());
//模板渲染
Context context = new Context();
context.setVariable("name", "HELLO_WORLD");
context.setVariable("code", code);
context.setVariable("sec", sec);
String mail = templateEngine.process("mail", context);
helper.setText(mail, true);
javaMailSender.send(msg);
System.out.println("郵件發(fā)送成功!");
} catch (MessagingException e) {
System.out.println("郵件發(fā)送失敗" + e.getMessage());
}
}
}
郵件模板[templates/mail.html]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>歡迎注冊(cè) HELLO_WORLD 網(wǎng)站!</title>
</head>
<style>
.big-font {
font-size: 25px;
}
.warning {
color: red;
background-color: bisque;
display: inline;
}
</style>
<body>
<h3>親愛的 [[${name}]],歡迎注冊(cè) HELLO_WORLD 網(wǎng)站!</h3>
<p>您的<b>注冊(cè)驗(yàn)證碼</b>是:<b class="big-font"> [[${code}]] </b></p>
<p>您的<b>識(shí)別碼</b>是:<b class="big-font"> [[${sec}]] </b></p>
<p class="warning">如果您并沒有注冊(cè) HELLO_WORLD 網(wǎng)站,請(qǐng)忽略該郵件!</p>
</body>
</html>
測試啟動(dòng)類及自測用例
import com.edu.vertifycode.mail.service.EmailService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = VerificationCodeMailApplication.class)
public class VerificationCodeMailApplicationTests {
@Resource
private EmailService emailService;
@Test
public void contextLoads() {
System.out.println("HELLO_WORLD!!!");
emailService.sendSignUpCaptcha(
"1836868464@qq.com",
"433999",
8848
);
}
}
自測效果截圖
文章來源地址http://www.zghlxwxcb.cn/news/detail-731283.html
文章來源:http://www.zghlxwxcb.cn/news/detail-731283.html
到了這里,關(guān)于SpringBoot整合郵箱發(fā)送郵件的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!