How to Activate New User Accounts by Email
In a previous article, I explained how to create an activation link for new users of a web app. In this article, I'll show you how to send that link via email using SMTP.
Join the DZone community and get the full member experience.
Join For FreeIn a previous article, I explained how to create an activation link for new users of a Vaadin web application. In this article, I'll show you how to send that link via email using SMTP.
Adding the spring-boot-starter-mail Dependency
Spring Boot makes it easy to start sending emails from the app. We just need to add the following to the <dependencies>
section of the pom.xml
file:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
Sending the Email Message
Now we can change the AuthService
class to send a message through email instead of printing it in the console. For that, we need a MailSender
instance that Spring Boot autoconfigures for us:
@Service
public class AuthService {
...
private final MailSender mailSender;
...
}
We can use this object in the register
method to effectively send an email message:
public void register(String email, String password) {
User user = userRepository.save(new User(email, password, Role.USER));
String text = "http://localhost:8080/activate?code=" + user.getActivationCode();
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("noreply@example.com");
message.setSubject("Confirmation email");
message.setText(text);
message.setTo(email);
mailSender.send(message);
}
Setting up a Test SMTP Server
In production, you will have to set up an SMTP server or use an existing one (like Gmail or Outlook) to send email messages. In your development environment, you can use something like FakeSMTP, a free SMTP server that incidentally is written in Java. Download it at http://nilhcem.com/FakeSMTP, run it, set the port to 9090, and start the server.
Configuring the SMTP Connection
In the application.properties
file add the following connection properties:
server.port=${PORT:8080}
spring.mail.host=localhost
spring.mail.port=9090
You can of course change the values to connect to your SMTP provider when you deploy the application to production (in which case you'll most likely need to add the spring.mail.password
property as well).
Fixing the Dependencies Issue
If you run the app at this point, you'll get an error. This is due to a dependency conflict. To solve it, add the following exclusion to the vaadin
dependency:
<dependency>
<groupId>com.vaadin</groupId>
<!-- Replace artifactId with vaadin-core to use only free components -->
<artifactId>vaadin</artifactId>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mailapi</artifactId>
</exclusion>
</exclusions>
</dependency>
Testing the App
That's it! To test the functionality, run the app using mvn
, create a new user using the app and see the email in the FakeSMTP window.
Opinions expressed by DZone contributors are their own.
Comments