Master Spring Boot Email Activation & Password Recovery: Full Code Guide
This article provides a comprehensive Spring Boot tutorial covering user account activation, email verification, password recovery, and advanced mailing features such as attachments and inline resources, complete with QQ mail configuration, Maven dependencies, JavaMailSender setup, asynchronous sending, and full code snippets for practical implementation.
1. Introduction
In many projects, features such as user registration, email activation and password recovery are widely used to improve user experience and enhance system security.
Account Activation
After registration the system sends an activation link via email; clicking the link changes the account status from inactive to active. The benefits are:
Prevent malicious registrations.
Verify the validity of the user's email address.
Increase user trust.
Password Recovery
The feature allows users who forget their password to reset it after verification, improving user experience and enhancing system security.
Improve user experience.
Enhance security through captcha, email verification, and other mechanisms.
2. Practical Example
This example uses the QQ mail server to send emails.
2.1 QQ Mail Configuration
Configure the QQ mailbox as follows (screenshots omitted).
2.2 Project Configuration
Add the mail starter dependency:
<code><dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency></code>Configure mail properties (application.yml):
<code>spring:
mail:
host: smtp.qq.com
username: [email protected]
# the authorization code generated in QQ mail, not the QQ password
password: xxxoooxxxooo
protocol: smtp
# port 465 does not work
port: 587
# additional timeout settings
properties:
'[mail.smtp.connectiontimeout]': 5000
'[mail.smtp.timeout]': 3000
'[mail.smtp.writetimeout]': 5000</code>2.3 Sending Template Email
Define a bean for the email template:
<code>@Bean
SimpleMailMessage mailMessage(MailProperties mailProperties) {
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setSubject("Spring Full Stack Demo – Account Activation");
mailMessage.setFrom(mailProperties.getUsername());
return mailMessage;
}</code>Service method to send the activation mail (asynchronous):
<code>@Resource
private JavaMailSender mailSender;
@Resource
private SimpleMailMessage templateMessage;
@Async
public void accountActivation(User user) {
SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage);
msg.setTo(user.getMail());
msg.setText("Thanks " + user.getName() + ", you have registered for the Spring Full Stack Demo. Please click the link to activate your account.");
try {
this.mailSender.send(msg);
} catch (MailException ex) {
System.err.printf("Mail send error: %s%n", ex.getMessage());
}
}</code>Test method:
<code>@Test
public void testAccountActivation() throws Exception {
User user = new User();
user.setMail("[email protected]");
user.setName("Pack");
user.setUsername("jaapack");
this.mailService.accountActivation(user);
System.in.read();
}</code>2.4 Sending Attachments
<code>@Async
public void sendAttachments(User user) throws Exception {
MimeMessage message = this.mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(mailProperties.getUsername());
helper.setTo(user.getMail());
helper.setSubject("Gift – Spring Boot Project Collection");
helper.setText("Thanks " + user.getName() + ", enjoy the Spring Boot project collection PDF.");
FileSystemResource file = new FileSystemResource(new File("f:/SpringBoot项目实战案例锦集.pdf"));
helper.addAttachment("SpringBoot项目实战案例锦集.pdf", file);
this.mailSender.send(message);
}</code>2.5 Sending Inline Resources
<code>@Async
public void sendInlineResource(User user) throws Exception {
MimeMessage message = this.mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(mailProperties.getUsername());
helper.setTo(user.getMail());
helper.setSubject("Inline Resource Demo");
helper.setText("Thanks <b>" + user.getName() + "</b>, check this image:<br/><img src='cid:id_un_123'/>", true);
FileSystemResource res = new FileSystemResource(new File("d:/images/1.png"));
helper.addInline("id_un_123", res);
this.mailSender.send(message);
}</code>Important: add the text content before adding inline resources, and set the second parameter of setText to true to indicate HTML content.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
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.