SpringBoot整合邮箱
1,开启POP3/SMTP/IMAP服务
注意:每个邮箱的密码唯一,不要随意分享给他人
最后就是这个样子了
2,整合测试
2.1,pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w5.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sin</groupId>
<artifactId>mail_massage</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mail_massage</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<!-- 短信业务 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.2,application.yml
spring:
mail:
# 配置SMTP服务器地址
host: smtp.126.com
# 配置端口号465或者25
port: 25
# 发送者邮箱(根据自己邮箱填写)
username: sin8023@126.com
# 配置密码,注意是开启POP3/SMTP的授权密码(根据自己的授权密码填写)
password: SZBSI****HNYAA*
properties:
mail:
smtp:
auth: true
enable: true
ssl:
# 设为true时 端口号设为 465 设为false时 端口号设为25
enable: false
socketFactoryClass: javax.net.ssl.SSLSocketFactory
#表示开启 DEBUG 模式,这样,邮件发送过程的日志会在控制台打印出来,方便排查错误
debug: true
# 邮箱编码格式
default-encoding: UTF-8
2.3,MailService.java
package com.sin.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
/**
* @CreateName SIN
* @CreateDate 2023/06/27 8:50
* @description
*/
@Service
public class MailService {
@Autowired(required = false)
private JavaMailSender mailSender;
@Value("${spring.mail.username")
private String userName;
/**
*
* @param to 收件人
* @param subject 邮箱标题
* @param content 邮箱内容
* @throws MessagingException
*/
public void sendMail(String to, String subject, String content) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
//邮箱发送者
helper.setFrom(userName);
//收件人,可以为多个收件人,收件人之间用逗号隔开
helper.setTo(to);
// 邮箱标题
helper.setSubject(subject);
// 邮箱内容
helper.setText(content, true);
mailSender.send(message);
}
}
2.4,MailController.java
package com.sin.controller;
import com.sin.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.mail.MessagingException;
/**
* @CreateName SIN
* @CreateDate 2023/06/27 8:50
* @description
*/
@RestController
public class MailController {
@Autowired
private MailService mailService;
@GetMapping("/sendMail")
public String sendMail() {
try {
mailService.sendMail("1824368699@qq.com", "测试邮箱", "使用SpringBoot整合邮箱发送消息");
return "邮件发送成功";
} catch (MessagingException e) {
e.printStackTrace();
return "邮件发送失败";
}
}
}
2.5,测试
2.6,查看结果
3,发送附件
3.1,MailFileServier.java
package com.sin.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
/**
* @CreateName SIN
* @CreateDate 2023/06/27 10:03
* @description
*/
@Service
public class MailFileServier {
@Autowired(required = false)
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String userName;
/**
*
* @param to 收件人
* @param subject 邮箱标题
* @param content 邮箱内容
* @throws MessagingException
*/
public void sendMail(String to, String subject, String content, String imgName, File file) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
//邮箱发送者
helper.setFrom(userName);
//收件人,可以为多个收件人,收件人之间用逗号隔开
helper.setTo(to);
// 邮箱标题
helper.setSubject(subject);
// 邮箱内容
helper.setText(content, true);
helper.addAttachment(imgName,file);
mailSender.send(message);
}
}
3.2,MailFileController.java
package com.sin.controller;
import com.sin.service.MailFileServier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.mail.MessagingException;
import java.io.File;
/**
* @CreateName SIN
* @CreateDate 2023/06/27 10:05
* @description
*/
@RestController
public class MailFileController {
@Autowired
private MailFileServier mailFileServier;
@GetMapping("/sendFile")
public String sendMail() {
try {
mailFileServier.sendMail("1824368699@qq.com", "测试邮箱", "使用SpringBoot整合邮箱发送消息",
"sql作业.txt" ,new File("E:\\SQL默写.txt"));
return "邮件发送成功";
} catch (MessagingException e) {
e.printStackTrace();
return "邮件发送失败";
}
}
}
3.3,测试
4,发送图片
4.1,MailImgServier.java
package com.sin.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
/**
* @CreateName SIN
* @CreateDate 2023/06/27 10:21
* @description
*/
@Service
public class MailImgService {
@Autowired(required = false)
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String userName;
/**
*
* @param to 收件人
* @param subject 邮箱标题
* @param content 邮箱内容
* @throws MessagingException
*/
public void sendMail(String to, String subject, String content, String imgName, FileSystemResource file) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
//邮箱发送者
helper.setFrom(userName);
//收件人,可以为多个收件人,收件人之间用逗号隔开
helper.setTo(to);
// 邮箱标题
helper.setSubject(subject);
// 邮箱内容
helper.setText(content, true);
helper.addInline(imgName,file);
mailSender.send(message);
}
}
4.2,MailImgController.java
package com.sin.controller;
import com.sin.service.MailFileServier;
import com.sin.service.MailImgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.mail.MessagingException;
import java.io.File;
/**
* @CreateName SIN
* @CreateDate 2023/06/27 10:21
* @description
*/
@RestController
public class MailImgController {
@Autowired
private MailImgService mailImgServier;
@GetMapping("/sendImg")
public String sendMail() {
try {
mailImgServier.sendMail("1824368699@qq.com", "测试邮箱",
"<h1>使用SpringBoot发送图片到邮箱</h1><p>图片:</p><img src='cid:img1'/>",
"img1" ,new FileSystemResource(new File("F:\\Users\\sin\\Pictures\\2.jpg")));
return "邮件发送成功";
} catch (MessagingException e) {
e.printStackTrace();
return "邮件发送失败";
}
}
}
4.3,测试
5,随机验证码
5.1,导入thymeleaf模板
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- Java提供的工具库 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>5.9</version>
</dependency>
5.2,application.yml
## thymeleaf模板设置
spring:
thymeleaf:
prefix: classpath:/templates/
suffix: .html
mode: HTML
encoding: utf-8
servlet.content-type: text/html
check-template-location: true #检查模板路径是否存在
cache: false
5.3,RandomCodeUtil.java
package com.sin.util;
import org.apache.commons.lang5.RandomStringUtils;
/**
* @CreateName SIN
* @CreateDate 2023/06/27 9:26
* @description 生成随机验证码
* 使用了Apache Commons Lang库中的RandomStringUtils类来生成随机数字字符串。
*/
public class RandomCodeUtil {
public static String generateCode(int length){
return RandomStringUtils.randomNumeric(length);
}
}
5.4,EmailCodeService.java
package com.sin.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
/**
* @CreateName SIN
* @CreateDate 2023/06/27 9:29
* @description
*/
@Service
public class EmailCodeService {
@Autowired(required = false)
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String userName;
public void sendSimpleMessage(String to, String subject, String text) {
SimpleMailMessage message = new SimpleMailMessage();
//邮箱发送者
message.setFrom(userName);
//收件人,可以为多个收件人,收件人之间用逗号隔开
message.setTo(to);
// 邮箱标题
message.setSubject(subject);
// 邮箱内容
message.setText(text);
mailSender.send(message);
}
}
5.5,EmailCodeController.java
package com.sin.controller;
import com.sin.service.EmailCodeService;
import com.sin.util.RandomCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @CreateName SIN
* @CreateDate 2023/06/27 9:30
* @description
*/
@Controller
public class EmailCodeController {
@Autowired
private EmailCodeService emailCodeService;
@GetMapping("/login")
public String home(Model model) {
// 生成6位随意验证码
String code = RandomCodeUtil.generateCode(6);
emailCodeService.sendSimpleMessage("1824368699@qq.com", "验证码", code);
model.addAttribute("code", code);
return "login.html";
}
}
5.6,测试
6,随机验证码登录
未完继续!!!!