Spring Boot QQ Email Registration and Login Tutorial with Postman Testing
This tutorial explains how to create a Spring Boot project that implements QQ email‑based user registration and login, covering POP3/SMTP setup, Maven dependencies, MySQL schema, core Java classes, configuration files, and step‑by‑step Postman testing procedures.
This article demonstrates how to build a QQ email registration and login system using Spring Boot without a front‑end, tested via Postman.
Login/registration logic: Registration sends a verification code to the supplied email, stores the code in the HTTP session, validates the code submitted by the user, and inserts the user record into a MySQL database; login queries the user by email and compares the stored password with the supplied one.
Preparation: Enable POP3/SMTP service in the QQ mailbox settings and save the authorization code (used later in application.properties ). Create a Spring Boot project with JDK 8 and add the following Maven dependencies:
<dependencies>
<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>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- jdbc -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
</dependencies>application.properties configuration:
# Email configuration
spring.mail.host = smtp.qq.com
spring.mail.username = [email protected]
spring.mail.password = xxxxxx
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.default-encoding=UTF-8
# Database configuration
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/email?useSSL=true&characterEncoding=utf-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
mybatis.mapper-locations=classpath:mapper/*.xmlDatabase schema:
CREATE DATABASE email;
CREATE TABLE `user` (
`id` int(20) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;Core project structure and classes:
Controller layer – UserController.java handles POST requests for sending email, registration and login.
Mapper layer – UserMapper.java defines insertUser and queryByEmail methods.
Entity – User.java represents the user table.
Service – MailService.java sends verification codes, validates them, saves users and performs login checks.
VO – UserVo.java carries form data; UserVoToUser.java converts it to a User entity.
MyBatis XML – UserMapper.xml contains the SQL statements for insertion and selection.
Key snippets:
@Controller
public class UserController {
@Autowired
private MailService mailService;
@PostMapping("/sendEmail")
@ResponseBody
public String sendEmail(String email, HttpSession httpSession){
mailService.sendMimeMail(email, httpSession);
return "success";
}
// regist and login methods omitted for brevity
} @Service
public class MailService {
@Autowired
private JavaMailSender mailSender;
@Autowired
private UserMapper userMapper;
@Value("${spring.mail.username}")
private String from;
public boolean sendMimeMail(String email, HttpSession session){
try {
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setSubject("验证码邮件");
String code = randomCode();
session.setAttribute("email", email);
session.setAttribute("code", code);
mailMessage.setText("您收到的验证码是:" + code);
mailMessage.setTo(email);
mailMessage.setFrom(from);
mailSender.send(mailMessage);
return true;
} catch (Exception e){
e.printStackTrace();
return false;
}
}
// randomCode, registered, loginIn methods omitted for brevity
}Testing with Postman: Use POST requests to the following URLs (replace the email, code and password with real values):
Send verification code: http://localhost:8080/[email protected]
Register user: http://localhost:8080/regist (POST body includes username , password , email , code )
Login: http://localhost:8080/[email protected]&password=yourPassword
Each request should be sent as POST; the responses will indicate success or failure.
Finally, the author invites readers to like the post if they find it helpful.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.