DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How To Build Web Service Using Spring Boot 2.x
  • Spring Boot Pet Clinic App: A Performance Study
  • Visually Designing Views for Java Web Apps
  • Spring Config Integration With a PCF Application: A Step-by-Step Guide

Trending

  • Chaos Engineering Has a Blind Spot. Agentic AI Lives in It.
  • Every Cache Miss Is a Tiny Tax on Your Performance
  • Stateless JWT Auth Microservice Architecture With Spring Boot 3 and Redis Sentinel
  • Event-Driven Pipelines With Apache Pulsar and Go
  1. DZone
  2. Coding
  3. Frameworks
  4. How to Activate New User Accounts by Email

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.

By 
Alejandro Duarte user avatar
Alejandro Duarte
DZone Core CORE ·
Jun. 13, 21 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
8.1K Views

Join the DZone community and get the full member experience.

Join For Free

In 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:

XML
 
<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:

Java
 
@Service
public class AuthService {
    ...
    private final MailSender mailSender;

    ...
}


We can use this object in the register method to effectively send an email message:

Java
 
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("[email protected]");
    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:

Properties files
 
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:

XML
 
<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.

Spring Framework app Spring Boot Activate (app) Dependency application Java (programming language) Property (programming) Testing

Opinions expressed by DZone contributors are their own.

Related

  • How To Build Web Service Using Spring Boot 2.x
  • Spring Boot Pet Clinic App: A Performance Study
  • Visually Designing Views for Java Web Apps
  • Spring Config Integration With a PCF Application: A Step-by-Step Guide

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook