目录
准备工作
依赖
yml配置
Config
Service层接口
ServiceImpl实现
Controller层
org.springframework.boot
spring-boot-starter-mail
spring:
mail:
# 你的邮箱地址
username: [email protected]
# 授权码
password: ******
# 默认的邮件编码为UTF-8
default-encoding: UTF-8
# 邮件服务器地址
host: smtp.qq.com
port: 465
properties:
mail:
transport:
protocol: smtps
smtps:
ssl:
enable: true
auth: true
starttls:
enable: true
package com.ma.springboot_test01.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import java.util.Properties;
/**
* @author Mtz
* @version 1.0
* @2023/4/910:40
* @function
* @comment
*/
@Configuration
@ConfigurationProperties(prefix = "spring.mail")
public class MailSenderConfig {
@Value("${spring.mail.host}")
private String host;
@Value("${spring.mail.port}")
private int port;
@Value("${spring.mail.username}")
private String username;
@Value("${spring.mail.password}")
private String password;
@Bean
public JavaMailSender mailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(host);
mailSender.setPort(port);
mailSender.setUsername(username);
mailSender.setPassword(password);
mailSender.setJavaMailProperties(getMailProperties());
return mailSender;
}
private Properties getMailProperties() {
Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtps");
properties.put("mail.smtps.auth", "true");
properties.put("mail.smtps.starttls.enable", "true");
properties.put("mail.smtps.ssl.enable", "true");
properties.put("mail.smtps.ssl.trust", host);
return properties;
}
}
int Register(String username, String password, String email, String verificationCode);
package com.ma.springboot_test01.service.impl;
import com.ma.springboot_test01.mapper.UserMapper;
import com.ma.springboot_test01.entity.User;
import com.ma.springboot_test01.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;
/**
* @author Mtz
* @version 1.0
* @2023/4/610:40
* @function
* @comment
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Autowired
private JavaMailSender mailSender;
@Override
public int Register(String username, String password, String email, String verificationCode) {
int register = userMapper.register(username, password, email, verificationCode);
return register;
}
// 发送验证码的方法
@Override
public void sendCode(String toEmail, String code) {
// 邮件内容
String subject = "欢迎注册";
String text = "您好,感谢您注册我们的服务。您的验证码为:" + code + ",请在5分钟内完成验证。";
// 实现 接口
JavaMailSenderImpl javaMailSender = (JavaMailSenderImpl) mailSender;
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(javaMailSender.getUsername());
message.setTo(toEmail);
message.setSubject(subject);
message.setText(text);
mailSender.send(message);
}
}
@PostMapping("/register")
public R register(@RequestBody User user) {
User login = userService.login(user.getUsername());
System.out.println("输入的数据:" + user.getUsername());
System.out.println("查询的数据" + login);
if (CheckData.isUsername(user.getUsername()) == false) {
return R.error("用户名不合法");
}
if (CheckData.isPassword(user.getPassword()) == false) {
return R.error("密码不合法");
}
if (CheckData.isQQEmail(user.getEmail()) == false) {
return R.error("邮箱不合法");
}
int register = userService.Register(user.getUsername(), user.getPassword(), user.getEmail(), user.getCaptchaValue());
return R.success(register);
}
/*
发送验证码!
*/
@PostMapping("/sendEmail")
public R sendEmail(@RequestBody User user) {
if (CheckData.isQQEmail(user.getEmail()) == false) {
return R.error("邮箱不合法");
}
// 生成随机验证码
String code = getSixCode();
if (!code.equals(user.getCaptchaValue())) {
R.error("验证码错误");
}
userService.sendCode(user.getEmail(), code);
return R.success("发送成功");
}
private String getSixCode() {
// 生成6位随机数字验证码
return String.valueOf((int) ((Math.random() * 9 + 1) * 100000));
}